@@ -85,6 +85,57 @@ const testChatWithMessageArrayCore = async function(model) {
8585 assert ( result . message . content . length > 0 , "message content should not be empty" ) ;
8686} ;
8787
88+ const testChatStreamingCore = async function ( model ) {
89+ // Test chat with streaming enabled
90+ const result = await puter . ai . chat ( "Count from 1 to 5" , {
91+ model : model ,
92+ stream : true ,
93+ max_tokens : 100
94+ } ) ;
95+
96+ // Check that result is an object and not null
97+ assert ( typeof result === 'object' , "streaming chat should return an object" ) ;
98+ assert ( result !== null , "streaming chat should not return null" ) ;
99+
100+ // For streaming, we need to check if it's an async iterator or has a different structure
101+ // The exact structure depends on the implementation, but we should verify it's consumable
102+ if ( result [ Symbol . asyncIterator ] ) {
103+ // If it's an async iterator, test that we can consume it
104+ let chunks = [ ] ;
105+ let chunkCount = 0 ;
106+ const maxChunks = 10 ; // Limit to prevent infinite loops in tests
107+
108+ for await ( const chunk of result ) {
109+ chunks . push ( chunk ) ;
110+ chunkCount ++ ;
111+
112+ // Verify each chunk has expected structure
113+ assert ( typeof chunk === 'object' , "each streaming chunk should be an object" ) ;
114+
115+ // Break after reasonable number of chunks for testing
116+ if ( chunkCount >= maxChunks ) break ;
117+ }
118+
119+ assert ( chunks . length > 0 , "streaming should produce at least one chunk" ) ;
120+
121+ } else {
122+ // If not an async iterator, it might be a different streaming implementation
123+ // Check for common streaming response patterns
124+
125+ // Check basic result structure (similar to non-streaming but may have different properties)
126+ assert ( typeof result . message === 'object' || typeof result . content === 'string' ,
127+ "streaming result should have message object or content string" ) ;
128+
129+ // Check that it has streaming-specific properties
130+ assert ( typeof result . stream === 'boolean' || result . stream === true ,
131+ "streaming result should indicate it's a stream" ) ;
132+
133+ // Check that toString() and valueOf() methods exist and work
134+ assert ( typeof result . toString === 'function' , "streaming result should have toString method" ) ;
135+ assert ( typeof result . valueOf === 'function' , "streaming result should have valueOf method" ) ;
136+ }
137+ } ;
138+
88139// Function to generate test functions for a specific model
89140const generateTestsForModel = function ( model ) {
90141 const modelName = model . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '_' ) ; // Sanitize model name for function names
@@ -128,6 +179,19 @@ const generateTestsForModel = function(model) {
128179 }
129180 }
130181 } ,
182+
183+ [ `testChatStreaming_${ modelName } ` ] : {
184+ name : `testChatStreaming_${ modelName } ` ,
185+ description : `Test AI chat with streaming enabled using ${ model } model` ,
186+ test : async function ( ) {
187+ try {
188+ await testChatStreamingCore ( model ) ;
189+ pass ( `testChatStreaming_${ modelName } passed` ) ;
190+ } catch ( error ) {
191+ fail ( `testChatStreaming_${ modelName } failed:` , error ) ;
192+ }
193+ }
194+ } ,
131195 } ;
132196} ;
133197
0 commit comments