|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | + |
| 3 | +async function setupChatPage(page) { |
| 4 | + // Mock capabilities endpoint so ModelSelector auto-selects a model |
| 5 | + await page.route('**/api/models/capabilities', (route) => { |
| 6 | + route.fulfill({ |
| 7 | + contentType: 'application/json', |
| 8 | + body: JSON.stringify({ |
| 9 | + data: [{ id: 'test-model', capabilities: ['FLAG_CHAT'] }], |
| 10 | + }), |
| 11 | + }) |
| 12 | + }) |
| 13 | +} |
| 14 | + |
| 15 | +test.describe('Chat - Error Handling', () => { |
| 16 | + test('shows backend error message on HTTP error', async ({ page }) => { |
| 17 | + await setupChatPage(page) |
| 18 | + |
| 19 | + await page.route('**/v1/chat/completions', (route) => { |
| 20 | + route.fulfill({ |
| 21 | + status: 500, |
| 22 | + contentType: 'application/json', |
| 23 | + body: JSON.stringify({ |
| 24 | + error: { message: 'Model failed to load', type: 'server_error', code: 500 }, |
| 25 | + }), |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + await page.goto('/app/chat') |
| 30 | + // Wait for the model to be auto-selected (ModelSelector shows model name in button) |
| 31 | + await expect(page.getByRole('button', { name: 'test-model' })).toBeVisible({ timeout: 10_000 }) |
| 32 | + |
| 33 | + await page.locator('.chat-input').fill('Hello') |
| 34 | + await page.locator('.chat-send-btn').click() |
| 35 | + |
| 36 | + await expect(page.locator('text=Model failed to load')).toBeVisible({ timeout: 10_000 }) |
| 37 | + }) |
| 38 | + |
| 39 | + test('shows error with trace link on HTTP error', async ({ page }) => { |
| 40 | + await setupChatPage(page) |
| 41 | + |
| 42 | + await page.route('**/v1/chat/completions', (route) => { |
| 43 | + route.fulfill({ |
| 44 | + status: 500, |
| 45 | + contentType: 'application/json', |
| 46 | + body: JSON.stringify({ |
| 47 | + error: { message: 'Backend crashed unexpectedly', type: 'server_error', code: 500 }, |
| 48 | + }), |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + await page.goto('/app/chat') |
| 53 | + await expect(page.getByRole('button', { name: 'test-model' })).toBeVisible({ timeout: 10_000 }) |
| 54 | + |
| 55 | + await page.locator('.chat-input').fill('Hello') |
| 56 | + await page.locator('.chat-send-btn').click() |
| 57 | + |
| 58 | + await expect(page.locator('text=Backend crashed unexpectedly')).toBeVisible({ timeout: 10_000 }) |
| 59 | + await expect(page.locator('.chat-error-trace-link')).toBeVisible() |
| 60 | + }) |
| 61 | + |
| 62 | + test('shows error from SSE error event during streaming', async ({ page }) => { |
| 63 | + await setupChatPage(page) |
| 64 | + |
| 65 | + await page.route('**/v1/chat/completions', (route) => { |
| 66 | + const body = [ |
| 67 | + 'data: {"choices":[{"delta":{"content":"Hello"},"index":0}]}\n\n', |
| 68 | + 'data: {"error":{"message":"Backend crashed mid-stream","type":"server_error"}}\n\n', |
| 69 | + 'data: [DONE]\n\n', |
| 70 | + ].join('') |
| 71 | + route.fulfill({ |
| 72 | + status: 200, |
| 73 | + headers: { 'Content-Type': 'text/event-stream' }, |
| 74 | + body, |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + await page.goto('/app/chat') |
| 79 | + await expect(page.getByRole('button', { name: 'test-model' })).toBeVisible({ timeout: 10_000 }) |
| 80 | + |
| 81 | + await page.locator('.chat-input').fill('Hello') |
| 82 | + await page.locator('.chat-send-btn').click() |
| 83 | + |
| 84 | + await expect(page.locator('text=Backend crashed mid-stream')).toBeVisible({ timeout: 10_000 }) |
| 85 | + }) |
| 86 | + |
| 87 | + test('shows generic HTTP error when no error body', async ({ page }) => { |
| 88 | + await setupChatPage(page) |
| 89 | + |
| 90 | + await page.route('**/v1/chat/completions', (route) => { |
| 91 | + route.fulfill({ |
| 92 | + status: 502, |
| 93 | + contentType: 'text/plain', |
| 94 | + body: 'Bad Gateway', |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + await page.goto('/app/chat') |
| 99 | + await expect(page.getByRole('button', { name: 'test-model' })).toBeVisible({ timeout: 10_000 }) |
| 100 | + |
| 101 | + await page.locator('.chat-input').fill('Hello') |
| 102 | + await page.locator('.chat-send-btn').click() |
| 103 | + |
| 104 | + await expect(page.locator('text=HTTP 502')).toBeVisible({ timeout: 10_000 }) |
| 105 | + }) |
| 106 | +}) |
0 commit comments