11import { beforeAll , describe , expect , it } from "vitest" ;
22import { OpenRouter } from "../../src/sdk/sdk.js" ;
3- import { ChatError } from "../../src/models/errors/chaterror.js" ;
43
54describe ( "Chat E2E Tests" , ( ) => {
65 let client : OpenRouter ;
@@ -115,6 +114,7 @@ describe("Chat E2E Tests", () => {
115114 const chunks : any [ ] = [ ] ;
116115
117116 for await ( const chunk of response ) {
117+ expect ( chunk ) . toBeDefined ( ) ;
118118 chunks . push ( chunk ) ;
119119 }
120120
@@ -154,7 +154,7 @@ describe("Chat E2E Tests", () => {
154154
155155 expect ( chunkCount ) . toBeGreaterThan ( 0 ) ;
156156 expect ( fullContent . length ) . toBeGreaterThan ( 0 ) ;
157- } ) ;
157+ } , 10000 )
158158
159159 it ( "should include finish_reason in final chunk" , async ( ) => {
160160 const response = await client . chat . send ( {
@@ -181,134 +181,6 @@ describe("Chat E2E Tests", () => {
181181 }
182182
183183 expect ( foundFinishReason ) . toBe ( true ) ;
184- } ) ;
185- } ) ;
186-
187- describe ( "Error Handling" , ( ) => {
188- it ( "should throw ErrorResponse with 401 status code for invalid API key" , async ( ) => {
189- const invalidClient = new OpenRouter ( {
190- apiKey : "invalid-api-key-12345" ,
191- } ) ;
192-
193- try {
194- await invalidClient . chat . send ( {
195- model : "meta-llama/llama-3.2-1b-instruct" ,
196- messages : [
197- {
198- role : "user" ,
199- content : "Hello" ,
200- } ,
201- ] ,
202- stream : false ,
203- } ) ;
204-
205- // If we reach here, the test should fail
206- expect . fail ( "Expected an error to be thrown for invalid API key" ) ;
207- } catch ( error ) {
208- // Verify it's an ErrorResponse
209- expect ( error ) . toBeInstanceOf ( ChatError ) ;
210-
211- if ( error instanceof ChatError ) {
212- // Verify status code is 401 (Unauthorized)
213- expect ( error . statusCode ) . toBe ( 401 ) ;
214-
215- // Verify error structure
216- expect ( error . error ) . toBeDefined ( ) ;
217- expect ( error . error . code ) . toBe ( 401 ) ;
218- expect ( error . error . message ) . toBeDefined ( ) ;
219- expect ( typeof error . error . message ) . toBe ( "string" ) ;
220-
221- // Verify the error message contains relevant information
222- expect ( error . message . toLowerCase ( ) ) . toMatch ( / i n v a l i d | u n a u t h o r i z e d | a p i k e y / i) ;
223- }
224- }
225- } ) ;
226-
227- it ( "should throw ErrorResponse with 400 status code for invalid model" , async ( ) => {
228- try {
229- await client . chat . send ( {
230- model : "this-model-does-not-exist/invalid-model-name-12345" ,
231- messages : [
232- {
233- role : "user" ,
234- content : "Hello" ,
235- } ,
236- ] ,
237- stream : false ,
238- } ) ;
239-
240- // If we reach here, the test should fail
241- expect . fail ( "Expected an error to be thrown for invalid model" ) ;
242- } catch ( error ) {
243- // Verify it's an ErrorResponse
244- expect ( error ) . toBeInstanceOf ( ChatError ) ;
245-
246- if ( error instanceof ChatError ) {
247- // Verify status code is 400 (Bad Request)
248- expect ( error . statusCode ) . toBe ( 400 ) ;
249-
250- // Verify error structure
251- expect ( error . error ) . toBeDefined ( ) ;
252- expect ( error . error . code ) . toBe ( 400 ) ;
253- expect ( error . error . message ) . toBeDefined ( ) ;
254- expect ( typeof error . error . message ) . toBe ( "string" ) ;
255-
256- // Verify the error message contains relevant information about invalid model
257- expect ( error . message . toLowerCase ( ) ) . toMatch ( / m o d e l | i n v a l i d | n o t f o u n d / i) ;
258- }
259- }
260- } ) ;
261-
262- it ( "should throw ErrorResponse with proper structure for invalid model in streaming mode" , async ( ) => {
263- await expect ( async ( ) => {
264- const response = await client . chat . send ( {
265- model : "this-model-does-not-exist/invalid-model-name-streaming" ,
266- messages : [
267- {
268- role : "user" ,
269- content : "Hello" ,
270- } ,
271- ] ,
272- stream : true ,
273- } ) ;
274-
275- // Consume the stream - error may be thrown here or during iteration
276- for await ( const _chunk of response ) {
277- // If we get chunks, that's unexpected
278- }
279- } ) . rejects . toThrow ( ) ;
280-
281- // Now test the actual error details
282- try {
283- const response = await client . chat . send ( {
284- model : "this-model-does-not-exist/invalid-model-name-streaming" ,
285- messages : [
286- {
287- role : "user" ,
288- content : "Hello" ,
289- } ,
290- ] ,
291- stream : true ,
292- } ) ;
293-
294- for await ( const _chunk of response ) {
295- // Continue consuming stream
296- }
297- } catch ( error ) {
298- // Verify it's an ErrorResponse
299- expect ( error ) . toBeInstanceOf ( ChatError ) ;
300-
301- if ( error instanceof ChatError ) {
302- // Verify status code is 400 (Bad Request)
303- expect ( error . statusCode ) . toBe ( 400 ) ;
304-
305- // Verify error structure
306- expect ( error . error ) . toBeDefined ( ) ;
307- expect ( error . error . code ) . toBe ( 400 ) ;
308- expect ( error . error . message ) . toBeDefined ( ) ;
309- expect ( typeof error . error . message ) . toBe ( "string" ) ;
310- }
311- }
312- } ) ;
184+ } , 10000 )
313185 } ) ;
314186} ) ;
0 commit comments