Add basic tests for puter.ai

This commit is contained in:
jelveh
2025-07-04 18:20:31 -07:00
parent 3eef18c578
commit 478aed7bda
2 changed files with 89 additions and 1 deletions
+58
View File
@@ -0,0 +1,58 @@
/* eslint-disable */
// TODO: Make these more compatible with eslint
window.aiTests = [
testChatBasicPrompt = async function() {
try {
// Test basic string prompt with test mode enabled
const result = await puter.ai.chat("Hello, how are you?");
// Check that result is an object and not null
assert(typeof result === 'object', "chat should return an object");
assert(result !== null, "chat should not return null");
// Check that the result has the expected structure
assert(typeof result.message === 'object', "result should have a message object");
assert(typeof result.message.content === 'string', "result.message should have content string");
// Check that toString() and valueOf() methods exist and work
assert(typeof result.toString === 'function', "result should have toString method");
assert(typeof result.valueOf === 'function', "result should have valueOf method");
// Check that toString() and valueOf() return the message content
assert(result.toString() === result.message.content, "toString() should return message content");
assert(result.valueOf() === result.message.content, "valueOf() should return message content");
// In test mode, the content should be a test response
assert(result.message.content.length > 0, "message content should not be empty");
pass("testChatBasicPrompt passed");
} catch (error) {
fail("testChatBasicPrompt failed:", error);
}
},
testChatWithParameters = async function() {
try {
// Test chat with parameters object
const result = await puter.ai.chat("What is 2+2?", {
model: "openrouter:openai/gpt-4.1-mini",
temperature: 0.7,
max_tokens: 50
});
// Check basic result structure
assert(typeof result === 'object', "chat should return an object");
assert(result !== null, "chat should not return null");
assert(typeof result.message === 'object', "result should have a message object");
assert(typeof result.message.content === 'string' || Array.isArray(result.message.content), "result.message should have content string or an array");
// Check that the methods work
assert(typeof result.toString === 'function', "result should have toString method");
assert(typeof result.valueOf === 'function', "result should have valueOf method");
pass("testChatWithParameters passed");
} catch (error) {
fail("testChatWithParameters failed:", error);
}
},
];
+31 -1
View File
@@ -1,9 +1,10 @@
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="../dist/puter.dev.js"></script>
<script src="https://js.puter.com/v2/"></script>
<script src="./kv.test.js"></script>
<script src="./fs.test.js"></script>
<script src="./ai.test.js"></script>
<style>
#tests {
margin-top: 20px;
@@ -77,6 +78,14 @@
</div>`);
}
$('#tests').append('<h2>AI Tests</h2>');
for (let i = 0; i < aiTests.length; i++) {
$('#tests').append(`<div class="test-container" id="aiTests-container-${i}">
<input type="checkbox" class="test-checkbox" id="aiTests${i}" checked>
<label for="aiTests${i}">${aiTests[i].name}</label><br>
</div>`);
}
window.assert = function(condition, message) {
if (!condition) {
throw new Error(message || "Assertion failed");
@@ -126,6 +135,27 @@
}
}
}
for (let i = 0; i < aiTests.length; i++) {
if (document.getElementById(`aiTests${i}`).checked) {
try{
await aiTests[i]();
// make this test's container green
$(`#aiTests-container-${i}`).css('background-color', '#85e085');
} catch (e) {
console.error('AI Test failed:', aiTests[i].name, e);
// make this test's container red
$(`#aiTests-container-${i}`).css('background-color', '#ff8484');
// message - show full error information including JSON details
let errorMessage = e.message || e.toString();
if (e.originalError) {
errorMessage += '\n\nOriginal Error:\n' + JSON.stringify(e.originalError, null, 2);
}
$(`#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>`);
}
}
}
}
$('#run-tests').click(() => {