ai-functions
Version:
A powerful TypeScript library for building AI-powered applications with template literals and structured outputs
84 lines • 3.3 kB
JavaScript
import { describe, expect, it, beforeEach } from 'vitest';
import { generateStreamingList } from './utils';
import { openai } from '@ai-sdk/openai';
describe('Streaming Utils', () => {
beforeEach(() => {
process.env.OPENAI_API_KEY = process.env.OPENAI_API_KEY || 'test-key';
});
describe('generateStreamingList', () => {
const model = openai('gpt-4o-mini');
it('should generate streaming list with basic options', async () => {
const options = {
model,
temperature: 0.7,
maxTokens: 100
};
const results = [];
for await (const item of generateStreamingList('List three colors', options)) {
results.push(item);
}
expect(results.length).toBeGreaterThan(0);
expect(results.every(item => typeof item === 'string')).toBe(true);
});
it('should handle streaming progress callback', async () => {
const progressUpdates = [];
const options = {
model,
streaming: {
onProgress: (chunk) => progressUpdates.push(chunk)
}
};
const results = [];
for await (const item of generateStreamingList('List three animals', options)) {
results.push(item);
}
expect(results.length).toBeGreaterThan(0);
expect(progressUpdates.length).toBeGreaterThan(0);
});
it('should handle model parameters', async () => {
const options = {
model,
temperature: 0.5,
maxTokens: 50,
topP: 0.9,
frequencyPenalty: 0.5,
presencePenalty: 0.5,
stop: ['END'],
seed: 42
};
const results = [];
for await (const item of generateStreamingList('List two fruits', options)) {
results.push(item);
}
expect(results.length).toBeGreaterThan(0);
});
it('should handle errors gracefully', async () => {
const options = {
model: undefined // Force fallback to default model
};
const results = [];
for await (const item of generateStreamingList('List items', options)) {
results.push(item);
}
expect(results.length).toBeGreaterThan(0);
expect(results.every(item => typeof item === 'string')).toBe(true);
});
it('should handle abort signal', async () => {
const controller = new AbortController();
const options = {
model,
signal: controller.signal
};
const promise = (async () => {
const results = [];
for await (const item of generateStreamingList('List many items', options)) {
results.push(item);
}
})();
// Abort after a short delay
setTimeout(() => controller.abort(), 100);
await expect(promise).rejects.toThrow('Stream was aborted');
});
});
});
//# sourceMappingURL=utils.test.js.map