|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | + |
| 3 | +test.describe('DataChannel chat', function () { |
| 4 | + test('can send and receive messages', async function ({ browser }) { |
| 5 | + const context = await browser.newContext(); |
| 6 | + const pageA = await context.newPage(); |
| 7 | + const pageB = await context.newPage(); |
| 8 | + |
| 9 | + // Both users join the same room |
| 10 | + await pageA.goto('/'); |
| 11 | + await pageA.fill('#room', 'chat-test-room'); |
| 12 | + await pageA.click('#join'); |
| 13 | + await expect(pageA.locator('#status')).toHaveText(/已加入|joined/i, { timeout: 5000 }); |
| 14 | + |
| 15 | + await pageB.goto('/'); |
| 16 | + await pageB.fill('#room', 'chat-test-room'); |
| 17 | + await pageB.click('#join'); |
| 18 | + await expect(pageB.locator('#status')).toHaveText(/已加入|joined/i, { timeout: 5000 }); |
| 19 | + |
| 20 | + // Establish call first (required for DataChannel) |
| 21 | + const memberB = await pageA.locator('.members__item').first().textContent(); |
| 22 | + if (memberB) { |
| 23 | + await pageA.fill('#remote', memberB.trim()); |
| 24 | + await pageA.click('#call'); |
| 25 | + |
| 26 | + // Wait for call to establish |
| 27 | + await pageA.waitForTimeout(3000); |
| 28 | + |
| 29 | + // Check if chat input exists and is visible |
| 30 | + const chatInput = pageA.locator('#chatInput'); |
| 31 | + if (await chatInput.isVisible({ timeout: 2000 }).catch(() => false)) { |
| 32 | + // Send a message from A to B |
| 33 | + await chatInput.fill('Hello from A!'); |
| 34 | + await pageA.click('#sendChat'); |
| 35 | + |
| 36 | + // Wait for message to arrive |
| 37 | + await pageB.waitForTimeout(1000); |
| 38 | + |
| 39 | + // Check if message appears on page B |
| 40 | + await expect(pageB.locator('.chat-messages, #messages')).toContainText('Hello from A!', { |
| 41 | + timeout: 5000 |
| 42 | + }); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + await context.close(); |
| 47 | + }); |
| 48 | + |
| 49 | + test('chat input is disabled when not in call', async function ({ page }) { |
| 50 | + await page.goto('/'); |
| 51 | + await page.fill('#room', 'chat-disabled-room'); |
| 52 | + await page.click('#join'); |
| 53 | + await expect(page.locator('#status')).toHaveText(/已加入|joined/i, { timeout: 5000 }); |
| 54 | + |
| 55 | + // Check if chat input is disabled when not in a call |
| 56 | + const chatInput = page.locator('#chatInput'); |
| 57 | + if (await chatInput.isVisible({ timeout: 1000 }).catch(() => false)) { |
| 58 | + // Chat input should exist but may be disabled |
| 59 | + const isDisabled = await chatInput.isDisabled().catch(() => true); |
| 60 | + // If not disabled, the send button might be disabled |
| 61 | + const sendButton = page.locator('#sendChat'); |
| 62 | + const sendDisabled = await sendButton.isDisabled().catch(() => true); |
| 63 | + expect(isDisabled || sendDisabled).toBeTruthy(); |
| 64 | + } |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +test.describe('DataChannel availability', function () { |
| 69 | + test('DataChannel is available during active call', async function ({ browser }) { |
| 70 | + const context = await browser.newContext(); |
| 71 | + const pageA = await context.newPage(); |
| 72 | + const pageB = await context.newPage(); |
| 73 | + |
| 74 | + // Setup call |
| 75 | + await pageA.goto('/'); |
| 76 | + await pageA.fill('#room', 'dc-avail-room'); |
| 77 | + await pageA.click('#join'); |
| 78 | + await expect(pageA.locator('#status')).toHaveText(/已加入|joined/i, { timeout: 5000 }); |
| 79 | + |
| 80 | + await pageB.goto('/'); |
| 81 | + await pageB.fill('#room', 'dc-avail-room'); |
| 82 | + await pageB.click('#join'); |
| 83 | + await expect(pageB.locator('#status')).toHaveText(/已加入|joined/i, { timeout: 5000 }); |
| 84 | + |
| 85 | + // Start call |
| 86 | + const memberB = await pageA.locator('.members__item').first().textContent(); |
| 87 | + if (memberB) { |
| 88 | + await pageA.fill('#remote', memberB.trim()); |
| 89 | + await pageA.click('#call'); |
| 90 | + |
| 91 | + // Wait for DataChannel to be ready |
| 92 | + await pageA.waitForTimeout(3000); |
| 93 | + |
| 94 | + // Check DataChannel state via console or UI |
| 95 | + // If there's a status indicator, check it |
| 96 | + const dcStatus = await pageA.locator('#dataChannelStatus, .dc-status').first(); |
| 97 | + if (await dcStatus.isVisible({ timeout: 1000 }).catch(() => false)) { |
| 98 | + await expect(dcStatus).toContainText(/open|ready|connected/i, { timeout: 5000 }); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + await context.close(); |
| 103 | + }); |
| 104 | +}); |
0 commit comments