@@ -4,15 +4,36 @@ window.aiTests = [
44 testChatBasicPrompt = async function ( ) {
55 try {
66 // Test basic string prompt with test mode enabled
7- const result = await puter . ai . chat ( "Hello, how are you?" ) ;
7+ const result = await puter . ai . chat ( "Hello, how are you?" , { model : "openrouter:openai/gpt-4.1-mini" } ) ;
88
99 // Check that result is an object and not null
1010 assert ( typeof result === 'object' , "chat should return an object" ) ;
1111 assert ( result !== null , "chat should not return null" ) ;
1212
13- // Check that the result has the expected structure
14- assert ( typeof result . message === 'object' , "result should have a message object" ) ;
15- assert ( typeof result . message . content === 'string' , "result.message should have content string" ) ;
13+ console . log ( 'result' , result ) ;
14+ // Check response structure
15+ assert ( typeof result . index === 'number' , "result should have index number" ) ;
16+ assert ( typeof result . message === 'object' , "result should have message object" ) ;
17+ assert ( typeof result . finish_reason === 'string' , "result should have finish_reason string" ) ;
18+ assert ( result . hasOwnProperty ( 'logprobs' ) , "result should have logprobs property" ) ;
19+ assert ( typeof result . via_ai_chat_service === 'boolean' , "result should have via_ai_chat_service boolean" ) ;
20+
21+ // Check message structure
22+ assert ( typeof result . message . role === 'string' , "message should have role string" ) ;
23+ assert ( result . message . role === 'assistant' , "message role should be 'assistant'" ) ;
24+ assert ( typeof result . message . content === 'string' , "message should have content string" ) ;
25+ assert ( result . message . hasOwnProperty ( 'refusal' ) , "message should have refusal property" ) ;
26+
27+ // Check usage tracking
28+ if ( result . usage ) {
29+ assert ( Array . isArray ( result . usage ) , "usage should be an array" ) ;
30+ result . usage . forEach ( usage => {
31+ assert ( typeof usage . type === 'string' , "usage should have type string" ) ;
32+ assert ( typeof usage . model === 'string' , "usage should have model string" ) ;
33+ assert ( typeof usage . amount === 'number' , "usage should have amount number" ) ;
34+ assert ( typeof usage . cost === 'number' , "usage should have cost number" ) ;
35+ } ) ;
36+ }
1637
1738 // Check that toString() and valueOf() methods exist and work
1839 assert ( typeof result . toString === 'function' , "result should have toString method" ) ;
@@ -22,7 +43,7 @@ window.aiTests = [
2243 assert ( result . toString ( ) === result . message . content , "toString() should return message content" ) ;
2344 assert ( result . valueOf ( ) === result . message . content , "valueOf() should return message content" ) ;
2445
25- // In test mode, the content should be a test response
46+ // Content should not be empty
2647 assert ( result . message . content . length > 0 , "message content should not be empty" ) ;
2748
2849 pass ( "testChatBasicPrompt passed" ) ;
@@ -43,16 +64,78 @@ window.aiTests = [
4364 // Check basic result structure
4465 assert ( typeof result === 'object' , "chat should return an object" ) ;
4566 assert ( result !== null , "chat should not return null" ) ;
46- assert ( typeof result . message === 'object' , "result should have a message object" ) ;
47- assert ( typeof result . message . content === 'string' || Array . isArray ( result . message . content ) , "result.message should have content string or an array " ) ;
67+ assert ( typeof result . message === 'object' , "result should have message object" ) ;
68+ assert ( typeof result . message . content === 'string' , "result.message should have content string" ) ;
4869
4970 // Check that the methods work
5071 assert ( typeof result . toString === 'function' , "result should have toString method" ) ;
5172 assert ( typeof result . valueOf === 'function' , "result should have valueOf method" ) ;
5273
74+ // Check that finish_reason is present and valid
75+ const validFinishReasons = [ 'stop' , 'length' , 'function_call' , 'content_filter' , 'tool_calls' ] ;
76+ assert ( validFinishReasons . includes ( result . finish_reason ) ,
77+ `finish_reason should be one of: ${ validFinishReasons . join ( ', ' ) } ` ) ;
78+
79+ // Check that via_ai_chat_service is true
80+ assert ( result . via_ai_chat_service === true , "via_ai_chat_service should be true" ) ;
81+
5382 pass ( "testChatWithParameters passed" ) ;
5483 } catch ( error ) {
5584 fail ( "testChatWithParameters failed:" , error ) ;
5685 }
5786 } ,
87+
88+ testChatWithMessageArray = async function ( ) {
89+ try {
90+ // Test chat with message array format
91+ const messages = [
92+ { role : "system" , content : "You are a helpful assistant." } ,
93+ { role : "user" , content : "Hello!" }
94+ ] ;
95+ const result = await puter . ai . chat ( messages ) ;
96+
97+ // Check basic structure
98+ assert ( typeof result === 'object' , "chat should return an object" ) ;
99+ assert ( typeof result . message === 'object' , "result should have message object" ) ;
100+ assert ( result . message . role === 'assistant' , "response should be from assistant" ) ;
101+
102+ // Check that content is present and not empty
103+ assert ( result . message . content . length > 0 , "message content should not be empty" ) ;
104+
105+ // Check that index is 0 (first/only response)
106+ assert ( result . index === 0 , "index should be 0 for single response" ) ;
107+
108+ pass ( "testChatWithMessageArray passed" ) ;
109+ } catch ( error ) {
110+ fail ( "testChatWithMessageArray failed:" , error ) ;
111+ }
112+ } ,
113+
114+ testChatUsageTracking = async function ( ) {
115+ try {
116+ // Test that usage tracking works correctly
117+ const result = await puter . ai . chat ( "Count to 5" ) ;
118+
119+ // Check usage tracking exists
120+ assert ( result . usage , "result should have usage tracking" ) ;
121+ assert ( Array . isArray ( result . usage ) , "usage should be an array" ) ;
122+ assert ( result . usage . length > 0 , "usage array should not be empty" ) ;
123+
124+ // Check for both prompt and completion usage
125+ const usageTypes = result . usage . map ( u => u . type ) ;
126+ assert ( usageTypes . includes ( 'prompt' ) , "usage should include prompt tracking" ) ;
127+ assert ( usageTypes . includes ( 'completion' ) , "usage should include completion tracking" ) ;
128+
129+ // Check that costs are calculated
130+ result . usage . forEach ( usage => {
131+ assert ( usage . amount > 0 , "usage amount should be greater than 0" ) ;
132+ assert ( usage . cost >= 0 , "usage cost should be non-negative" ) ;
133+ assert ( typeof usage . model === 'string' , "usage should track model used" ) ;
134+ } ) ;
135+
136+ pass ( "testChatUsageTracking passed" ) ;
137+ } catch ( error ) {
138+ fail ( "testChatUsageTracking failed:" , error ) ;
139+ }
140+ } ,
58141] ;
0 commit comments