Skip to content

Commit af9bf09

Browse files
committed
Add puter.ai streaming test
1 parent a2d05af commit af9bf09

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/puter-js/src/modules/PuterDialog.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ class PuterDialog extends HTMLElement {
456456
}
457457

458458
open() {
459-
console.log(this.hasUserActivation());
460459
if(this.hasUserActivation()){
461460
let w = 600;
462461
let h = 400;

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
89140
const generateTestsForModel = function(model) {
90141
const modelName = model.replace(/[^a-zA-Z0-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

src/puter-js/test/run.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<html>
22
<head>
33
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
4-
<script src="http://puter.localhost:4100/sdk/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>
77
<script src="./ai.test.js"></script>

0 commit comments

Comments
 (0)