|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | + |
| 3 | +/** |
| 4 | + * E2E tests for SmolLM2 browser inference. |
| 5 | + * |
| 6 | + * These tests verify that the WASM-based language model can: |
| 7 | + * 1. Load successfully in the browser |
| 8 | + * 2. Generate text responses without errors |
| 9 | + * 3. Stream tokens back to the UI |
| 10 | + * |
| 11 | + * Note: These tests require significant time due to: |
| 12 | + * - Model download (~270MB) |
| 13 | + * - WASM compilation |
| 14 | + * - Inference computation |
| 15 | + */ |
| 16 | + |
| 17 | +test.describe('SmolLM2 Browser Inference', () => { |
| 18 | + // Run tests serially since they share model state |
| 19 | + test.describe.configure({ mode: 'serial' }); |
| 20 | + |
| 21 | + test('should display initial UI correctly', async ({ page }) => { |
| 22 | + await page.goto('/'); |
| 23 | + |
| 24 | + // Check header |
| 25 | + await expect(page.getByRole('heading', { name: 'SmolLM2 in Browser' })).toBeVisible(); |
| 26 | + await expect( |
| 27 | + page.getByText('AI language model running entirely on your device via WebAssembly') |
| 28 | + ).toBeVisible(); |
| 29 | + |
| 30 | + // Check load button |
| 31 | + await expect(page.getByRole('button', { name: /Load Model/i })).toBeVisible(); |
| 32 | + await expect(page.getByRole('button', { name: /Load Model/i })).toBeEnabled(); |
| 33 | + |
| 34 | + // Check initial message |
| 35 | + await expect( |
| 36 | + page.getByText(/Hello! I'm SmolLM2, a small language model running entirely in your browser/) |
| 37 | + ).toBeVisible(); |
| 38 | + |
| 39 | + // Check footer info |
| 40 | + await expect(page.getByText(/No data sent to servers/)).toBeVisible(); |
| 41 | + |
| 42 | + // Check initial status - worker sends 'Worker initialized' on startup |
| 43 | + await expect(page.getByText('Worker initialized')).toBeVisible(); |
| 44 | + }); |
| 45 | + |
| 46 | + test('should load model successfully', async ({ page }) => { |
| 47 | + await page.goto('/'); |
| 48 | + |
| 49 | + // Verify initial state |
| 50 | + await expect(page.getByRole('button', { name: /Load Model/i })).toBeVisible(); |
| 51 | + |
| 52 | + // Click load button |
| 53 | + await page.getByRole('button', { name: /Load Model/i }).click(); |
| 54 | + |
| 55 | + // Should show loading status |
| 56 | + await expect(page.getByText(/Initializing|Downloading|Loading/i)).toBeVisible({ |
| 57 | + timeout: 10000, |
| 58 | + }); |
| 59 | + |
| 60 | + // Wait for model to be ready (this can take several minutes) |
| 61 | + await expect(page.getByText('Model ready')).toBeVisible({ |
| 62 | + timeout: 5 * 60 * 1000, // 5 minutes |
| 63 | + }); |
| 64 | + |
| 65 | + // Load button should be gone |
| 66 | + await expect(page.getByRole('button', { name: /Load Model/i })).not.toBeVisible(); |
| 67 | + |
| 68 | + // Message input should be enabled |
| 69 | + await expect(page.locator('.cs-message-input__content-editor')).toBeEnabled(); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should generate text response without errors', async ({ page }) => { |
| 73 | + await page.goto('/'); |
| 74 | + |
| 75 | + // Listen for console errors from the start |
| 76 | + const consoleErrors: string[] = []; |
| 77 | + page.on('console', (msg) => { |
| 78 | + if (msg.type() === 'error') { |
| 79 | + consoleErrors.push(msg.text()); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + // Load the model first |
| 84 | + await page.getByRole('button', { name: /Load Model/i }).click(); |
| 85 | + await expect(page.getByText('Model ready')).toBeVisible({ |
| 86 | + timeout: 5 * 60 * 1000, |
| 87 | + }); |
| 88 | + |
| 89 | + // Send a message |
| 90 | + const messageInput = page.locator('.cs-message-input__content-editor'); |
| 91 | + await messageInput.fill('Hello'); |
| 92 | + await messageInput.press('Enter'); |
| 93 | + |
| 94 | + // Should show user message |
| 95 | + await expect(page.getByText('Hello').first()).toBeVisible(); |
| 96 | + |
| 97 | + // Wait for generation to complete (typing indicator should appear then disappear) |
| 98 | + // The response should appear within 2 minutes |
| 99 | + await expect(page.getByText('SmolLM2 is thinking...')).toBeVisible({ timeout: 10000 }); |
| 100 | + |
| 101 | + // Wait for typing indicator to disappear (generation complete) |
| 102 | + await expect(page.getByText('SmolLM2 is thinking...')).not.toBeVisible({ |
| 103 | + timeout: 2 * 60 * 1000, |
| 104 | + }); |
| 105 | + |
| 106 | + // Check for the critical error that was reported in issue #5 |
| 107 | + const repeatPenaltyError = consoleErrors.find((e) => |
| 108 | + e.includes('Repeat penalty failed: unexpected rank') |
| 109 | + ); |
| 110 | + expect(repeatPenaltyError).toBeUndefined(); |
| 111 | + |
| 112 | + // There should be no error status |
| 113 | + await expect(page.getByText(/Error:/i)).not.toBeVisible(); |
| 114 | + |
| 115 | + // Status should still be "Model ready" (not error state) |
| 116 | + await expect(page.getByText('Model ready')).toBeVisible(); |
| 117 | + }); |
| 118 | + |
| 119 | + test('should stream tokens to the UI', async ({ page }) => { |
| 120 | + await page.goto('/'); |
| 121 | + |
| 122 | + // Load the model first |
| 123 | + await page.getByRole('button', { name: /Load Model/i }).click(); |
| 124 | + await expect(page.getByText('Model ready')).toBeVisible({ |
| 125 | + timeout: 5 * 60 * 1000, |
| 126 | + }); |
| 127 | + |
| 128 | + // Send a message |
| 129 | + const messageInput = page.locator('.cs-message-input__content-editor'); |
| 130 | + await messageInput.fill('Count from 1 to 5'); |
| 131 | + await messageInput.press('Enter'); |
| 132 | + |
| 133 | + // Wait for generation to complete |
| 134 | + await expect(page.getByText('SmolLM2 is thinking...')).toBeVisible({ timeout: 10000 }); |
| 135 | + await expect(page.getByText('SmolLM2 is thinking...')).not.toBeVisible({ |
| 136 | + timeout: 2 * 60 * 1000, |
| 137 | + }); |
| 138 | + |
| 139 | + // There should be multiple AI response regions (initial greeting + new response) |
| 140 | + const aiMessages = page.locator('[class*="cs-message--incoming"]'); |
| 141 | + await expect(aiMessages).toHaveCount(2, { timeout: 5000 }); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | +test.describe('Error Handling', () => { |
| 146 | + test('should handle model loading gracefully', async ({ page }) => { |
| 147 | + await page.goto('/'); |
| 148 | + |
| 149 | + // Click load button |
| 150 | + await page.getByRole('button', { name: /Load Model/i }).click(); |
| 151 | + |
| 152 | + // Should not crash immediately |
| 153 | + await expect(page.getByText(/Initializing|Downloading/i)).toBeVisible({ |
| 154 | + timeout: 10000, |
| 155 | + }); |
| 156 | + |
| 157 | + // Page should remain responsive |
| 158 | + await expect(page.getByRole('heading', { name: 'SmolLM2 in Browser' })).toBeVisible(); |
| 159 | + }); |
| 160 | +}); |
0 commit comments