/** * DragTest — 联机 E2E * * 流程: * - Tab 0 创建并 join P0 * - Tab 1 刷新后 join P1 * - 验证双方都看到 5 张牌在 hand */ import { test, expect, Browser } from '@playwright/test'; async function setupOnlineGame(browser: Browser, gameKey: string) { const ctx = await browser.newContext(); const page = await ctx.newPage(); await page.goto('/'); await page.getByTestId('game-select').selectOption(gameKey); await page.getByTestId('mode-online').click(); await expect(page.getByTestId('lobby-room-list')).toBeVisible(); return page; } async function joinAsFirstPlayer(page: any, playerSlot: '0' | '1') { if (playerSlot === '0') { await page.getByTestId('lobby-create-room').click(); } else { // Tab 1: 等房间列表里出现 room await expect(async () => { await page.getByTestId('lobby-refresh').click(); const roomChips = await page.locator('[data-testid^="lobby-room-"]').all(); const ids = await Promise.all(roomChips.map((c: any) => c.getAttribute('data-testid'))); const real = ids.filter((id: string | null) => id && !id.endsWith('-list') && !id.endsWith('-refresh') && !id.endsWith('-room'), ); expect(real.length).toBeGreaterThan(0); }).toPass({ timeout: 10_000 }); const rooms = page.locator('[data-testid^="lobby-room-"]:not([data-testid="lobby-room-list"])'); const count = await rooms.count(); expect(count).toBeGreaterThan(0); const firstRoom = rooms.first(); const matchIDAttr = await firstRoom.getAttribute('data-testid'); const matchID = matchIDAttr!.replace(/^lobby-room-/, ''); await page.getByTestId(`lobby-join-1-${matchID}`).click(); } await expect(page.getByTestId('online-need-room')).not.toBeVisible({ timeout: 15_000 }); } test.describe('DragTest — 2-player multiplayer E2E', () => { test('both clients see the same 5-card hand initially', async ({ browser }) => { const p0 = await setupOnlineGame(browser, 'drag-test'); const p1 = await setupOnlineGame(browser, 'drag-test'); await joinAsFirstPlayer(p0, '0'); await joinAsFirstPlayer(p1, '1'); // 双方都看到 hand zone 含 5 张牌 await expect(p0.getByTestId('zone-hand')).toHaveAttribute('data-count', '5'); await expect(p1.getByTestId('zone-hand')).toHaveAttribute('data-count', '5'); }); });