Skip to content

Commit 9698761

Browse files
committed
AND MOOOAREEE AI tests
1 parent 3fdee77 commit 9698761

File tree

2 files changed

+145
-134
lines changed

2 files changed

+145
-134
lines changed

src/puter-js/test/ai.test.js

Lines changed: 133 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,140 @@
11
/* eslint-disable */
22
// TODO: Make these more compatible with eslint
3-
window.aiTests = [
4-
testChatBasicPrompt = async function() {
5-
try {
6-
// Test basic string prompt with test mode enabled
7-
const result = await puter.ai.chat("Hello, how are you?", { model: "openrouter:openai/gpt-4.1-mini" });
8-
9-
// Check that result is an object and not null
10-
assert(typeof result === 'object', "chat should return an object");
11-
assert(result !== null, "chat should not return null");
12-
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");
263

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-
});
4+
// Define models to test
5+
const TEST_MODELS = [
6+
"openrouter:openai/gpt-4.1-mini",
7+
"openrouter:anthropic/claude-3.5-sonnet-20240620",
8+
"gpt-4o-mini",
9+
"claude-sonnet-4-latest",
10+
// Add more models as needed
11+
];
12+
13+
// Core test functions that can be reused across models
14+
const testChatBasicPromptCore = async function(model) {
15+
// Test basic string prompt with test mode enabled
16+
const result = await puter.ai.chat("Hello, how are you?", { model: model });
17+
18+
// Check that result is an object and not null
19+
assert(typeof result === 'object', "chat should return an object");
20+
assert(result !== null, "chat should not return null");
21+
22+
// Check response structure
23+
assert(typeof result.message === 'object', "result should have message object");
24+
assert(typeof result.finish_reason === 'string', "result should have finish_reason string");
25+
assert(typeof result.via_ai_chat_service === 'boolean', "result should have via_ai_chat_service boolean");
26+
27+
// Check message structure
28+
assert(typeof result.message.role === 'string', "message should have role string");
29+
assert(result.message.role === 'assistant', "message role should be 'assistant'");
30+
assert(typeof result.message.content === 'string' || Array.isArray(result.message.content), "message should have content string or an array");
31+
32+
// Check that toString() and valueOf() methods exist and work
33+
assert(typeof result.toString === 'function', "result should have toString method");
34+
assert(typeof result.valueOf === 'function', "result should have valueOf method");
35+
36+
// Check that toString() and valueOf() return the message content
37+
assert(result.toString() === result.message.content, "toString() should return message content");
38+
assert(result.valueOf() === result.message.content, "valueOf() should return message content");
39+
40+
// Content should not be empty
41+
assert(result.message.content.length > 0, "message content should not be empty");
42+
};
43+
44+
const testChatWithParametersCore = async function(model) {
45+
// Test chat with parameters object
46+
const result = await puter.ai.chat("What is 2+2?", {
47+
model: model,
48+
temperature: 0.7,
49+
max_tokens: 50
50+
});
51+
52+
// Check basic result structure
53+
assert(typeof result === 'object', "chat should return an object");
54+
assert(result !== null, "chat should not return null");
55+
assert(typeof result.message === 'object', "result should have message object");
56+
assert(typeof result.message.content === 'string' || Array.isArray(result.message.content), "result.message should have content string or an array");
57+
58+
// Check that the methods work
59+
assert(typeof result.toString === 'function', "result should have toString method");
60+
assert(typeof result.valueOf === 'function', "result should have valueOf method");
61+
62+
// Check that finish_reason is present and valid
63+
const validFinishReasons = ['stop', 'length', 'function_call', 'content_filter', 'tool_calls'];
64+
assert(validFinishReasons.includes(result.finish_reason),
65+
`finish_reason should be one of: ${validFinishReasons.join(', ')}`);
66+
67+
// Check that via_ai_chat_service is true
68+
assert(result.via_ai_chat_service === true, "via_ai_chat_service should be true");
69+
};
70+
71+
const testChatWithMessageArrayCore = async function(model) {
72+
// Test chat with message array format
73+
const messages = [
74+
{ role: "system", content: "You are a helpful assistant." },
75+
{ role: "user", content: "Hello!" }
76+
];
77+
const result = await puter.ai.chat(messages, { model: model });
78+
79+
// Check basic structure
80+
assert(typeof result === 'object', "chat should return an object");
81+
assert(typeof result.message === 'object', "result should have message object");
82+
assert(result.message.role === 'assistant', "response should be from assistant");
83+
84+
// Check that content is present and not empty
85+
assert(result.message.content.length > 0, "message content should not be empty");
86+
87+
// Check that index is 0 (first/only response)
88+
assert(result.index === 0, "index should be 0 for single response");
89+
};
90+
91+
// Function to generate test functions for a specific model
92+
const generateTestsForModel = function(model) {
93+
const modelName = model.replace(/[^a-zA-Z0-9]/g, '_'); // Sanitize model name for function names
94+
95+
return {
96+
[`testChatBasicPrompt_${modelName}`]: async function() {
97+
try {
98+
await testChatBasicPromptCore(model);
99+
pass(`testChatBasicPrompt_${modelName} passed`);
100+
} catch (error) {
101+
fail(`testChatBasicPrompt_${modelName} failed:`, error);
102+
}
103+
},
104+
105+
[`testChatWithParameters_${modelName}`]: async function() {
106+
try {
107+
await testChatWithParametersCore(model);
108+
pass(`testChatWithParameters_${modelName} passed`);
109+
} catch (error) {
110+
fail(`testChatWithParameters_${modelName} failed:`, error);
36111
}
37-
38-
// Check that toString() and valueOf() methods exist and work
39-
assert(typeof result.toString === 'function', "result should have toString method");
40-
assert(typeof result.valueOf === 'function', "result should have valueOf method");
41-
42-
// Check that toString() and valueOf() return the message content
43-
assert(result.toString() === result.message.content, "toString() should return message content");
44-
assert(result.valueOf() === result.message.content, "valueOf() should return message content");
45-
46-
// Content should not be empty
47-
assert(result.message.content.length > 0, "message content should not be empty");
48-
49-
pass("testChatBasicPrompt passed");
50-
} catch (error) {
51-
fail("testChatBasicPrompt failed:", error);
52-
}
53-
},
112+
},
113+
114+
[`testChatWithMessageArray_${modelName}`]: async function() {
115+
try {
116+
await testChatWithMessageArrayCore(model);
117+
pass(`testChatWithMessageArray_${modelName} passed`);
118+
} catch (error) {
119+
fail(`testChatWithMessageArray_${modelName} failed:`, error);
120+
}
121+
},
122+
};
123+
};
54124

55-
testChatWithParameters = async function() {
56-
try {
57-
// Test chat with parameters object
58-
const result = await puter.ai.chat("What is 2+2?", {
59-
model: "openrouter:openai/gpt-4.1-mini",
60-
temperature: 0.7,
61-
max_tokens: 50
62-
});
63-
64-
// Check basic result structure
65-
assert(typeof result === 'object', "chat should return an object");
66-
assert(result !== null, "chat should not return null");
67-
assert(typeof result.message === 'object', "result should have message object");
68-
assert(typeof result.message.content === 'string', "result.message should have content string");
69-
70-
// Check that the methods work
71-
assert(typeof result.toString === 'function', "result should have toString method");
72-
assert(typeof result.valueOf === 'function', "result should have valueOf method");
73-
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-
82-
pass("testChatWithParameters passed");
83-
} catch (error) {
84-
fail("testChatWithParameters failed:", error);
85-
}
86-
},
125+
// Generate all test functions for all models
126+
const generateAllTests = function() {
127+
const allTests = [];
87128

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-
},
129+
TEST_MODELS.forEach(model => {
130+
const modelTests = generateTestsForModel(model);
131+
Object.values(modelTests).forEach(testFunc => {
132+
allTests.push(testFunc);
133+
});
134+
});
113135

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-
},
141-
];
136+
return allTests;
137+
};
138+
139+
// Export the generated tests
140+
window.aiTests = generateAllTests();

src/puter-js/test/run.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
padding: 10px;
3636
border-radius: 5px;
3737
}
38+
.test-container{
39+
font-family: monospace;
40+
}
41+
.test-container:hover{
42+
background-color: #f0f0f0;
43+
}
44+
.test-container label{
45+
display: block;
46+
}
47+
.test-container input{
48+
float: left;
49+
}
3850
</style>
3951
<script>
4052
document.addEventListener("DOMContentLoaded", () => {

0 commit comments

Comments
 (0)