|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 2 | +import { |
| 3 | + evaluateConversation, |
| 4 | + defaultProductDetailsCriteria, |
| 5 | + IEvaluationResult, |
| 6 | + IConversationTurn, |
| 7 | +} from './evaluator'; |
| 8 | +import { runConversation, IConversationScenario } from './conversationRunner'; |
| 9 | +import { clearLastRunDirectory, saveFailedTest } from './testResultsReporter'; |
| 10 | +import { setupTestProducts, teardownTestProducts } from './testFixtures'; |
| 11 | + |
| 12 | +const MINIMUM_PASSING_SCORE = 3.5; |
| 13 | + |
| 14 | +beforeAll(async () => { |
| 15 | + clearLastRunDirectory(); |
| 16 | + await setupTestProducts(); |
| 17 | +}, 60000); |
| 18 | + |
| 19 | +afterAll(async () => { |
| 20 | + await teardownTestProducts(); |
| 21 | +}, 30000); |
| 22 | + |
| 23 | +const productDetailsScenarios: IConversationScenario[] = [ |
| 24 | + { |
| 25 | + name: 'Product details by position', |
| 26 | + locale: 'en', |
| 27 | + turns: [ |
| 28 | + { userMessage: 'Show me laptops' }, |
| 29 | + { userMessage: 'What are the specs of the first one?' }, |
| 30 | + ], |
| 31 | + expectedBehavior: |
| 32 | + 'After showing laptops, the assistant should provide detailed specifications of the first laptop including RAM, processor, storage from attributes or description.', |
| 33 | + }, |
| 34 | + { |
| 35 | + name: 'Product details by name', |
| 36 | + locale: 'en', |
| 37 | + turns: [{ userMessage: 'Tell me about Gaming Laptop Pro X1' }], |
| 38 | + expectedBehavior: |
| 39 | + 'The assistant should provide detailed information about the Gaming Laptop Pro X1 including specifications like RAM, GPU, and storage.', |
| 40 | + }, |
| 41 | + { |
| 42 | + name: 'Product details in Polish', |
| 43 | + locale: 'pl', |
| 44 | + turns: [ |
| 45 | + { userMessage: 'Pokaż laptopy' }, |
| 46 | + { userMessage: 'Jaki procesor ma pierwszy?' }, |
| 47 | + ], |
| 48 | + expectedBehavior: |
| 49 | + 'The assistant should provide processor details of the first laptop in Polish language.', |
| 50 | + }, |
| 51 | + { |
| 52 | + name: 'Non-existent product', |
| 53 | + locale: 'en', |
| 54 | + turns: [{ userMessage: 'Tell me about SuperPhone 3000' }], |
| 55 | + expectedBehavior: |
| 56 | + 'The assistant should indicate that the product was not found or ask for more information.', |
| 57 | + }, |
| 58 | + { |
| 59 | + name: 'Product details by partial name', |
| 60 | + locale: 'en', |
| 61 | + turns: [{ userMessage: 'What specs does the iPhone have?' }], |
| 62 | + expectedBehavior: |
| 63 | + 'The assistant should provide details about the iPhone 15 Pro Max including processor and storage.', |
| 64 | + }, |
| 65 | +]; |
| 66 | + |
| 67 | +describe('Product Details E2E Evaluation', () => { |
| 68 | + describe.each(productDetailsScenarios)('Scenario: $name', (scenario) => { |
| 69 | + let evaluationResult: IEvaluationResult; |
| 70 | + let conversation: IConversationTurn[]; |
| 71 | + |
| 72 | + beforeAll(async () => { |
| 73 | + const conversationResult = await runConversation(scenario); |
| 74 | + conversation = conversationResult.conversation; |
| 75 | + |
| 76 | + console.log(`\n=== Conversation: ${scenario.name} ===`); |
| 77 | + conversation.forEach((turn) => { |
| 78 | + console.log(`${turn.role.toUpperCase()}: ${turn.content}`); |
| 79 | + }); |
| 80 | + |
| 81 | + expect(conversationResult.success).toBe(true); |
| 82 | + |
| 83 | + evaluationResult = await evaluateConversation( |
| 84 | + conversation, |
| 85 | + defaultProductDetailsCriteria, |
| 86 | + scenario.expectedBehavior |
| 87 | + ); |
| 88 | + |
| 89 | + console.log(`\nEvaluation Score: ${evaluationResult.score}`); |
| 90 | + console.log(`Reasoning: ${evaluationResult.reasoning}\n`); |
| 91 | + |
| 92 | + if (evaluationResult.score < MINIMUM_PASSING_SCORE) { |
| 93 | + saveFailedTest(scenario, conversation, evaluationResult); |
| 94 | + } |
| 95 | + }, 180000); |
| 96 | + |
| 97 | + it('should pass LLM evaluation with score >= 3.5', () => { |
| 98 | + expect(evaluationResult.score).toBeGreaterThanOrEqual(MINIMUM_PASSING_SCORE); |
| 99 | + expect(evaluationResult.passed).toBe(true); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should have valid reasoning', () => { |
| 103 | + expect(evaluationResult.reasoning).toBeTruthy(); |
| 104 | + expect(evaluationResult.reasoning.length).toBeGreaterThan(10); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +const MULTI_TURN_COMPLEX_MIN_SCORE = 3.0; |
| 110 | + |
| 111 | +const multiTurnDetailsScenarios: Array<{ |
| 112 | + scenario: IConversationScenario; |
| 113 | + minScore: number; |
| 114 | +}> = [ |
| 115 | + { |
| 116 | + scenario: { |
| 117 | + name: 'Search then ask for multiple products', |
| 118 | + locale: 'en', |
| 119 | + turns: [ |
| 120 | + { userMessage: 'Show me smartphones' }, |
| 121 | + { userMessage: 'Tell me more about the first one' }, |
| 122 | + { userMessage: 'What about the second one?' }, |
| 123 | + ], |
| 124 | + expectedBehavior: |
| 125 | + 'The assistant should show smartphones first, then provide details for the first smartphone, then provide details for the second smartphone. Each product should have specifications.', |
| 126 | + }, |
| 127 | + minScore: MULTI_TURN_COMPLEX_MIN_SCORE, |
| 128 | + }, |
| 129 | + { |
| 130 | + scenario: { |
| 131 | + name: 'Search then compare', |
| 132 | + locale: 'en', |
| 133 | + turns: [ |
| 134 | + { userMessage: 'I need a laptop' }, |
| 135 | + { userMessage: 'How much RAM does the first one have?' }, |
| 136 | + ], |
| 137 | + expectedBehavior: |
| 138 | + 'The assistant should first show laptops, then provide the RAM specification for the first laptop when asked.', |
| 139 | + }, |
| 140 | + minScore: MINIMUM_PASSING_SCORE, |
| 141 | + }, |
| 142 | +]; |
| 143 | + |
| 144 | +describe('Multi-Turn Product Details E2E Evaluation', () => { |
| 145 | + describe.each(multiTurnDetailsScenarios)('Scenario: $scenario.name', ({ scenario, minScore }) => { |
| 146 | + let evaluationResult: IEvaluationResult; |
| 147 | + let conversation: IConversationTurn[]; |
| 148 | + |
| 149 | + beforeAll(async () => { |
| 150 | + const conversationResult = await runConversation(scenario); |
| 151 | + conversation = conversationResult.conversation; |
| 152 | + |
| 153 | + console.log(`\n=== Multi-Turn: ${scenario.name} ===`); |
| 154 | + conversation.forEach((turn) => { |
| 155 | + console.log(`${turn.role.toUpperCase()}: ${turn.content}`); |
| 156 | + }); |
| 157 | + |
| 158 | + expect(conversationResult.success).toBe(true); |
| 159 | + |
| 160 | + evaluationResult = await evaluateConversation( |
| 161 | + conversation, |
| 162 | + defaultProductDetailsCriteria, |
| 163 | + scenario.expectedBehavior |
| 164 | + ); |
| 165 | + |
| 166 | + console.log(`\nEvaluation Score: ${evaluationResult.score}`); |
| 167 | + console.log(`Reasoning: ${evaluationResult.reasoning}\n`); |
| 168 | + |
| 169 | + if (evaluationResult.score < minScore) { |
| 170 | + saveFailedTest(scenario, conversation, evaluationResult); |
| 171 | + } |
| 172 | + }, 240000); |
| 173 | + |
| 174 | + it(`should pass LLM evaluation with score >= ${minScore}`, () => { |
| 175 | + expect(evaluationResult.score).toBeGreaterThanOrEqual(minScore); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should have valid reasoning', () => { |
| 179 | + expect(evaluationResult.reasoning).toBeTruthy(); |
| 180 | + expect(evaluationResult.reasoning.length).toBeGreaterThan(10); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments