loop-pattern.ts
1 /** 2 * Loop Pattern Integration Test 3 * 4 * Tests the Loop workflow pattern with real data. 5 * 6 * Run: npx ts-node loop-pattern.ts 7 */ 8 9 import { Loop, loopPattern, Agent, Workflow } from '../../../src/praisonai-ts/dist'; 10 11 async function main() { 12 console.log('=== Loop Pattern Integration Test ===\n'); 13 14 // Test 1: Basic Loop with array 15 console.log('1. Testing Loop with array iteration:'); 16 const items = ['apple', 'banana', 'cherry']; 17 18 const processor = (item: string) => `Processed: ${item.toUpperCase()}`; 19 const arrayLoop = new Loop(processor, { over: 'items' }); 20 21 const result1 = await arrayLoop.run({ items }); 22 console.log(' Results:', result1.results); 23 console.log(' Iterations:', result1.iterations); 24 console.log(' Success:', result1.success ? '✅' : '❌'); 25 26 // Test 2: Loop with convenience function 27 console.log('\n2. Testing loop() convenience function:'); 28 const doubler = (n: number) => n * 2; 29 const numberLoop = loopPattern(doubler, { over: 'numbers' }); 30 31 const result2 = await numberLoop.run({ numbers: [1, 2, 3, 4, 5] }); 32 console.log(' Input: [1, 2, 3, 4, 5]'); 33 console.log(' Output:', result2.results); 34 console.log(' Success:', result2.success ? '✅' : '❌'); 35 36 // Test 3: Loop with Agent (requires OPENAI_API_KEY) 37 if (process.env.OPENAI_API_KEY) { 38 console.log('\n3. Testing Loop with Agent (live API):'); 39 try { 40 const agent = new Agent({ 41 name: 'Summarizer', 42 instructions: 'Summarize the given topic in one sentence.', 43 llm: 'openai/gpt-4o-mini' 44 }); 45 46 const topics = ['AI', 'TypeScript']; 47 const agentLoop = new Loop(agent, { over: 'topics', varName: 'topic' }); 48 49 console.log(' Processing topics:', topics); 50 const result3 = await agentLoop.run({ topics }); 51 52 result3.results.forEach((res, i) => { 53 console.log(` [${topics[i]}]: ${res.slice(0, 80)}...`); 54 }); 55 console.log(' Success:', result3.success ? '✅' : '❌'); 56 } catch (error: any) { 57 console.log(' ⚠️ Agent test failed:', error.message); 58 } 59 } else { 60 console.log('\n3. Agent Loop Test: Skipped (OPENAI_API_KEY not set)'); 61 } 62 63 // Test 4: Loop with error handling 64 console.log('\n4. Testing Loop with continueOnError:'); 65 const errorProneProcessor = (item: string) => { 66 if (item === 'fail') throw new Error('Intentional failure'); 67 return `OK: ${item}`; 68 }; 69 70 const errorLoop = new Loop(errorProneProcessor, { 71 over: 'items', 72 continueOnError: true 73 }); 74 75 const result4 = await errorLoop.run({ items: ['a', 'fail', 'b'] }); 76 console.log(' Results:', result4.results); 77 console.log(' Errors:', result4.errors.length); 78 console.log(' Success:', result4.success ? '✅' : '❌ (expected - has errors)'); 79 80 // Test 5: Loop type exports 81 console.log('\n5. Verifying exports:'); 82 console.log(' Loop class:', typeof Loop); 83 console.log(' loop function:', typeof loopPattern); 84 85 console.log('\n=== Loop Pattern Tests Complete ==='); 86 } 87 88 main().catch(console.error);