Skip to content

Commit 478aed7

Browse files
committed
Add basic tests for puter.ai
1 parent 3eef18c commit 478aed7

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* eslint-disable */
2+
// 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?");
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+
// 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");
16+
17+
// Check that toString() and valueOf() methods exist and work
18+
assert(typeof result.toString === 'function', "result should have toString method");
19+
assert(typeof result.valueOf === 'function', "result should have valueOf method");
20+
21+
// Check that toString() and valueOf() return the message content
22+
assert(result.toString() === result.message.content, "toString() should return message content");
23+
assert(result.valueOf() === result.message.content, "valueOf() should return message content");
24+
25+
// In test mode, the content should be a test response
26+
assert(result.message.content.length > 0, "message content should not be empty");
27+
28+
pass("testChatBasicPrompt passed");
29+
} catch (error) {
30+
fail("testChatBasicPrompt failed:", error);
31+
}
32+
},
33+
34+
testChatWithParameters = async function() {
35+
try {
36+
// Test chat with parameters object
37+
const result = await puter.ai.chat("What is 2+2?", {
38+
model: "openrouter:openai/gpt-4.1-mini",
39+
temperature: 0.7,
40+
max_tokens: 50
41+
});
42+
43+
// Check basic result structure
44+
assert(typeof result === 'object', "chat should return an object");
45+
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");
48+
49+
// Check that the methods work
50+
assert(typeof result.toString === 'function', "result should have toString method");
51+
assert(typeof result.valueOf === 'function', "result should have valueOf method");
52+
53+
pass("testChatWithParameters passed");
54+
} catch (error) {
55+
fail("testChatWithParameters failed:", error);
56+
}
57+
},
58+
];

src/puter-js/test/run.html

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<html>
22
<head>
33
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
4-
<script src="../dist/puter.dev.js"></script>
4+
<script src="https://js.puter.com/v2/"></script>
55
<script src="./kv.test.js"></script>
66
<script src="./fs.test.js"></script>
7+
<script src="./ai.test.js"></script>
78
<style>
89
#tests {
910
margin-top: 20px;
@@ -77,6 +78,14 @@
7778
</div>`);
7879
}
7980

81+
$('#tests').append('<h2>AI Tests</h2>');
82+
for (let i = 0; i < aiTests.length; i++) {
83+
$('#tests').append(`<div class="test-container" id="aiTests-container-${i}">
84+
<input type="checkbox" class="test-checkbox" id="aiTests${i}" checked>
85+
<label for="aiTests${i}">${aiTests[i].name}</label><br>
86+
</div>`);
87+
}
88+
8089
window.assert = function(condition, message) {
8190
if (!condition) {
8291
throw new Error(message || "Assertion failed");
@@ -126,6 +135,27 @@
126135
}
127136
}
128137
}
138+
139+
for (let i = 0; i < aiTests.length; i++) {
140+
if (document.getElementById(`aiTests${i}`).checked) {
141+
try{
142+
await aiTests[i]();
143+
// make this test's container green
144+
$(`#aiTests-container-${i}`).css('background-color', '#85e085');
145+
146+
} catch (e) {
147+
console.error('AI Test failed:', aiTests[i].name, e);
148+
// make this test's container red
149+
$(`#aiTests-container-${i}`).css('background-color', '#ff8484');
150+
// message - show full error information including JSON details
151+
let errorMessage = e.message || e.toString();
152+
if (e.originalError) {
153+
errorMessage += '\n\nOriginal Error:\n' + JSON.stringify(e.originalError, null, 2);
154+
}
155+
$(`#aiTests-container-${i}`).append(`<pre style="color:red; white-space: pre-wrap; font-size: 12px; margin: 5px 0; padding: 10px; background-color: #f8f8f8; border-radius: 3px;">${errorMessage}</pre>`);
156+
}
157+
}
158+
}
129159
}
130160

131161
$('#run-tests').click(() => {

0 commit comments

Comments
 (0)