|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | + |
| 3 | +test.describe('Connecting', () => { |
| 4 | + test('should connect and list conversations', async ({ page }) => { |
| 5 | + // Go to the app |
| 6 | + await page.goto('/'); |
| 7 | + |
| 8 | + // Should show demo conversations initially |
| 9 | + await expect(page.getByText('Introduction to gptme')).toBeVisible(); |
| 10 | + |
| 11 | + // Should show connection status |
| 12 | + const connectionButton = page.getByRole('button', { name: /Connect/i }); |
| 13 | + await expect(connectionButton).toBeVisible(); |
| 14 | + |
| 15 | + // Click the demo conversation |
| 16 | + await page.getByText('Introduction to gptme').click(); |
| 17 | + |
| 18 | + // Should show the conversation content |
| 19 | + await expect(page.getByText(/Hello! I'm gptme, your AI programming assistant/)).toBeVisible(); |
| 20 | + await page.goto('/'); |
| 21 | + |
| 22 | + // Should show demo conversations immediately |
| 23 | + await expect(page.getByText('Introduction to gptme')).toBeVisible(); |
| 24 | + |
| 25 | + // Wait for successful connection |
| 26 | + await expect(page.getByRole('button', { name: /Connect/i })).toHaveClass(/text-green-600/, { |
| 27 | + timeout: 10000, |
| 28 | + }); |
| 29 | + |
| 30 | + // Wait for success toast to confirm API connection |
| 31 | + await expect(page.getByText('Connected to gptme server')).toBeVisible(); |
| 32 | + |
| 33 | + // Wait for conversations to load |
| 34 | + // Should show both demo conversations and connected conversations |
| 35 | + await expect(page.getByText('Introduction to gptme')).toBeVisible(); |
| 36 | + |
| 37 | + // Wait for loading state to finish |
| 38 | + await expect(page.getByText('Loading conversations...')).toBeHidden(); |
| 39 | + |
| 40 | + // Get the conversation list |
| 41 | + const conversationList = page.getByTestId('conversation-list'); |
| 42 | + |
| 43 | + // Get all conversation titles |
| 44 | + const conversationTitles = await conversationList |
| 45 | + .locator('[data-testid="conversation-title"]') |
| 46 | + .allTextContents(); |
| 47 | + |
| 48 | + // Should have both demo and API conversations |
| 49 | + const demoConversations = conversationTitles.filter((title) => title.includes('Introduction')); |
| 50 | + const apiConversations = conversationTitles.filter((title) => /^\d+$/.test(title)); |
| 51 | + |
| 52 | + expect(demoConversations.length).toBeGreaterThan(0); |
| 53 | + |
| 54 | + if (apiConversations.length > 0) { |
| 55 | + // Check for historical timestamps if we have API conversations |
| 56 | + const timestamps = await conversationList |
| 57 | + .getByRole('button') |
| 58 | + .locator('time') |
| 59 | + .allTextContents(); |
| 60 | + expect(timestamps.length).toBeGreaterThan(1); |
| 61 | + |
| 62 | + // There should be some timestamps that aren't "just now" |
| 63 | + const nonJustNowTimestamps = timestamps.filter((t) => t !== 'just now'); |
| 64 | + expect(nonJustNowTimestamps.length).toBeGreaterThan(0); |
| 65 | + } else { |
| 66 | + // This happens when e2e tests are run in CI with a fresh gptme-server |
| 67 | + console.log('No API conversations found, skipping timestamp check'); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + test('should handle connection errors gracefully', async ({ page }) => { |
| 72 | + // Start with server unavailable |
| 73 | + await page.goto('/'); |
| 74 | + |
| 75 | + // Should still show demo conversations |
| 76 | + await expect(page.getByText('Introduction to gptme')).toBeVisible(); |
| 77 | + |
| 78 | + // Click connect button and try to connect to non-existent server |
| 79 | + const connectionButton = page.getByRole('button', { name: /Connect/i }); |
| 80 | + await connectionButton.click(); |
| 81 | + |
| 82 | + // Fill in invalid server URL and try to connect |
| 83 | + await page.getByLabel('Server URL').fill('http://localhost:1'); |
| 84 | + await page.getByRole('button', { name: /^(Connect|Reconnect)$/ }).click(); |
| 85 | + |
| 86 | + // Wait for error toast to appear |
| 87 | + await expect(page.getByText('Could not connect to gptme instance')).toBeVisible({ |
| 88 | + timeout: 10000, |
| 89 | + }); |
| 90 | + |
| 91 | + // Close the connection dialog by clicking outside |
| 92 | + await page.keyboard.press('Escape'); |
| 93 | + |
| 94 | + // Verify connection button is in disconnected state |
| 95 | + await expect(connectionButton).toBeVisible(); |
| 96 | + await expect(connectionButton).not.toHaveClass(/text-green-600/); |
| 97 | + |
| 98 | + // Should show demo conversations |
| 99 | + await expect(page.getByText('Introduction to gptme')).toBeVisible(); |
| 100 | + |
| 101 | + // Should not show any API conversations |
| 102 | + const conversationList = page.getByTestId('conversation-list'); |
| 103 | + const conversationTitles = await conversationList |
| 104 | + .locator('[data-testid="conversation-title"]') |
| 105 | + .allTextContents(); |
| 106 | + |
| 107 | + const apiConversations = conversationTitles.filter((title) => /^\d+$/.test(title)); |
| 108 | + expect(apiConversations.length).toBe(0); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +test.describe('Conversation Flow', () => { |
| 113 | + test('should be able to create a new conversation and send a message', async ({ page }) => { |
| 114 | + await page.goto('/'); |
| 115 | + |
| 116 | + // Click the "New Conversation" button to start a new conversation |
| 117 | + await page.locator('[data-testid="new-conversation-button"]').click(); |
| 118 | + |
| 119 | + // Wait for the new conversation page to load |
| 120 | + await expect(page).toHaveURL(/\?conversation=\d+$/); |
| 121 | + |
| 122 | + const message = 'Hello. We are testing, just say exactly "Hello world" without anything else.'; |
| 123 | + |
| 124 | + // Type a message |
| 125 | + await page.getByRole('textbox').fill(message); |
| 126 | + await page.keyboard.press('Enter'); |
| 127 | + |
| 128 | + // Should show the message in the conversation |
| 129 | + // Look specifically for the user's message in a user message container |
| 130 | + await expect( |
| 131 | + page.locator('.role-user', { |
| 132 | + hasText: message, |
| 133 | + }) |
| 134 | + ).toBeVisible(); |
| 135 | + |
| 136 | + // Should show the AI's response |
| 137 | + await expect( |
| 138 | + page.locator('.role-assistant', { |
| 139 | + hasText: 'Hello world', |
| 140 | + }) |
| 141 | + ).toBeVisible(); |
| 142 | + }); |
| 143 | +}); |
0 commit comments