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

websocket.test.ts 6.5 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  1. import WebSocket from 'ws';
  2. import { WebSocketProvider, createTransformResponse } from '../../src/providers/websocket';
  3. jest.mock('ws');
  4. describe('createTransformResponse', () => {
  5. it('should use provided function parser', () => {
  6. const parser = (data: any) => ({ output: `parsed-${data}` });
  7. const transform = createTransformResponse(parser);
  8. expect(transform('test')).toEqual({ output: 'parsed-test' });
  9. });
  10. it('should create function from string parser', () => {
  11. const parser = '({ output: `parsed-${data}` })';
  12. const transform = createTransformResponse(parser);
  13. expect(transform('test')).toEqual({ output: 'parsed-test' });
  14. });
  15. it('should return default transform if no parser provided', () => {
  16. const transform = createTransformResponse(undefined);
  17. expect(transform('test')).toEqual({ output: 'test' });
  18. });
  19. });
  20. describe('WebSocketProvider', () => {
  21. let mockWs: jest.Mocked<WebSocket>;
  22. let provider: WebSocketProvider;
  23. beforeEach(() => {
  24. mockWs = {
  25. on: jest.fn(),
  26. send: jest.fn(),
  27. close: jest.fn(),
  28. onmessage: jest.fn(),
  29. onerror: jest.fn(),
  30. onopen: jest.fn(),
  31. } as unknown as jest.Mocked<WebSocket>;
  32. jest.mocked(WebSocket).mockImplementation(() => mockWs);
  33. provider = new WebSocketProvider('ws://test.com', {
  34. config: {
  35. messageTemplate: '{{ prompt }}',
  36. timeoutMs: 1000,
  37. },
  38. });
  39. });
  40. afterEach(() => {
  41. jest.clearAllMocks();
  42. });
  43. it('should initialize with correct config', () => {
  44. expect(provider.url).toBe('ws://test.com');
  45. expect(provider.config.messageTemplate).toBe('{{ prompt }}');
  46. });
  47. it('should pass headers to WebSocket connection', async () => {
  48. const headers = {
  49. Authorization: 'Bearer test-token',
  50. 'Custom-Header': 'test-value',
  51. };
  52. provider = new WebSocketProvider('ws://test.com', {
  53. config: {
  54. messageTemplate: '{{ prompt }}',
  55. headers,
  56. },
  57. });
  58. // Mock WebSocket to handle the connection properly
  59. jest.mocked(WebSocket).mockImplementation(() => {
  60. setTimeout(() => {
  61. mockWs.onopen?.({ type: 'open', target: mockWs } as WebSocket.Event);
  62. setTimeout(() => {
  63. mockWs.onmessage?.({
  64. data: JSON.stringify({ result: 'test' }),
  65. } as WebSocket.MessageEvent);
  66. }, 10);
  67. }, 10);
  68. return mockWs;
  69. });
  70. // Trigger the WebSocket connection by calling callApi
  71. await provider.callApi('test prompt');
  72. // Now assert that WebSocket was called with the headers
  73. expect(WebSocket).toHaveBeenCalledWith('ws://test.com', { headers });
  74. });
  75. it('should work without headers provided', async () => {
  76. provider = new WebSocketProvider('ws://test.com', {
  77. config: {
  78. messageTemplate: '{{ prompt }}',
  79. // No headers provided
  80. },
  81. });
  82. // Mock WebSocket to handle the connection properly
  83. jest.mocked(WebSocket).mockImplementation(() => {
  84. setTimeout(() => {
  85. mockWs.onopen?.({ type: 'open', target: mockWs } as WebSocket.Event);
  86. setTimeout(() => {
  87. mockWs.onmessage?.({
  88. data: JSON.stringify({ result: 'test' }),
  89. } as WebSocket.MessageEvent);
  90. }, 10);
  91. }, 10);
  92. return mockWs;
  93. });
  94. // Trigger the WebSocket connection by calling callApi
  95. const response = await provider.callApi('test prompt');
  96. // Should still work and return expected response
  97. expect(response).toEqual({ output: { output: { result: 'test' } } });
  98. // Headers should be undefined when not provided
  99. expect(WebSocket).toHaveBeenCalledWith('ws://test.com', { headers: undefined });
  100. });
  101. it('should throw if messageTemplate is missing', () => {
  102. expect(() => {
  103. new WebSocketProvider('ws://test.com', {
  104. config: {},
  105. });
  106. }).toThrow(
  107. 'Expected WebSocket provider ws://test.com to have a config containing {messageTemplate}',
  108. );
  109. });
  110. it('should send message and handle response', async () => {
  111. const responseData = { result: 'test response' };
  112. jest.mocked(WebSocket).mockImplementation(() => {
  113. setTimeout(() => {
  114. mockWs.onopen?.({ type: 'open', target: mockWs } as WebSocket.Event);
  115. setTimeout(() => {
  116. mockWs.onmessage?.({ data: JSON.stringify(responseData) } as WebSocket.MessageEvent);
  117. }, 10);
  118. }, 10);
  119. return mockWs;
  120. });
  121. const response = await provider.callApi('test prompt');
  122. expect(response).toEqual({ output: { output: responseData } });
  123. });
  124. it('should handle WebSocket errors', async () => {
  125. jest.mocked(WebSocket).mockImplementation(() => {
  126. setTimeout(() => {
  127. mockWs.onerror?.({
  128. type: 'error',
  129. error: new Error('connection failed'),
  130. message: 'connection failed',
  131. } as WebSocket.ErrorEvent);
  132. }, 10);
  133. return mockWs;
  134. });
  135. const response = await provider.callApi('test prompt');
  136. expect(response.error).toContain('WebSocket error');
  137. });
  138. it('should handle timeout', async () => {
  139. provider = new WebSocketProvider('ws://test.com', {
  140. config: {
  141. messageTemplate: '{{ prompt }}',
  142. timeoutMs: 100,
  143. },
  144. });
  145. const response = await provider.callApi('test prompt');
  146. expect(response).toEqual({ error: 'WebSocket request timed out' });
  147. });
  148. it('should handle non-JSON response', async () => {
  149. jest.mocked(WebSocket).mockImplementation(() => {
  150. setTimeout(() => {
  151. mockWs.onopen?.({ type: 'open', target: mockWs } as WebSocket.Event);
  152. setTimeout(() => {
  153. mockWs.onmessage?.({ data: 'plain text response' } as WebSocket.MessageEvent);
  154. }, 10);
  155. }, 10);
  156. return mockWs;
  157. });
  158. const response = await provider.callApi('test prompt');
  159. expect(response).toEqual({ output: { output: 'plain text response' } });
  160. });
  161. it('should use custom response transformer', async () => {
  162. provider = new WebSocketProvider('ws://test.com', {
  163. config: {
  164. messageTemplate: '{{ prompt }}',
  165. transformResponse: (data: any) => ({ output: `transformed-${data}` }),
  166. },
  167. });
  168. jest.mocked(WebSocket).mockImplementation(() => {
  169. setTimeout(() => {
  170. mockWs.onopen?.({ type: 'open', target: mockWs } as WebSocket.Event);
  171. setTimeout(() => {
  172. mockWs.onmessage?.({ data: 'test' } as WebSocket.MessageEvent);
  173. }, 10);
  174. }, 10);
  175. return mockWs;
  176. });
  177. const response = await provider.callApi('test prompt');
  178. expect(response).toEqual({ output: { output: 'transformed-test' } });
  179. });
  180. });
Tip!

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

Comments

Loading...