#!/usr/bin/env node /** * 端切换双向验证(2026-11-10) * * 复现用户实测的完整流程: * ① 桌面端宽屏 → 缩窄 → 提示切移动端 ✅ * ② 点「切换」→ 进入移动端 * ③ **把窗口放大回桌面尺寸 → 必须提示切回桌面端**(用户反馈这里失效) * ④ 点「切换」→ 回到桌面端 */ import { chromium } from 'playwright'; import { mkdir } from 'node:fs/promises'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const here = dirname(fileURLToPath(import.meta.url)); const outDir = resolve(here, '..', 'test-results', 'switch-both-ways'); const BASE = process.env.BASE_URL || 'http://127.0.0.1:4300'; await mkdir(outDir, { recursive: true }); const browser = await chromium.launch({ channel: 'chrome' }); const page = await browser.newPage({ viewport: { width: 1440, height: 900 } }); const errors = []; page.on('pageerror', (e) => errors.push(String(e))); const results = []; await page.goto(`${BASE}/desktop/chat`, { waitUntil: 'domcontentloaded' }); await page.waitForTimeout(8000); // ① 缩窄 → 桌面端提示 await page.setViewportSize({ width: 600, height: 900 }); await page.waitForTimeout(900); const hint1 = await page.locator('.narrow-desktop').count(); results.push(['① 桌面缩窄 → 提示切移动端', hint1 === 1, `hint=${hint1}`]); // ② 点切换 → 进入移动端 const goMobile = page.locator('.narrow-btn', { hasText: '切换到手机端' }); if (await goMobile.count()) { await goMobile.first().click(); await page.waitForTimeout(2500); } const onMobile = page.url().includes('/mobile'); results.push(['② 点切换 → 进入移动端', onMobile, page.url()]); // ③ 🔴 放大回桌面尺寸 → 移动端应提示切回桌面 await page.setViewportSize({ width: 1440, height: 900 }); await page.waitForTimeout(1200); const hint2 = await page.locator('.wide-mobile').count(); await page.screenshot({ path: `${outDir}/1-mobile-wide-hint.png` }); results.push(['③ 🔴 放大 → 提示切回桌面端', hint2 === 1, `hint=${hint2}`]); // ④ 点切换 → 回到桌面端 const goDesktop = page.locator('.wide-btn', { hasText: '切换到桌面端' }); if (await goDesktop.count()) { await goDesktop.first().click(); await page.waitForTimeout(2500); } const backDesktop = page.url().includes('/desktop'); await page.screenshot({ path: `${outDir}/2-back-desktop.png` }); results.push(['④ 点切换 → 回到桌面端', backDesktop, page.url()]); // ⑤ 回到桌面后不应再提示 await page.waitForTimeout(1000); const hint3 = await page.locator('.narrow-desktop').count(); results.push(['⑤ 已切回桌面 → 不再提示', hint3 === 0, `hint=${hint3}`]); let pass = true; console.log('\n=== 结果 ==='); for (const [name, ok, detail] of results) { if (!ok) pass = false; console.log(`${ok ? '✓' : '✗'} ${name} (${detail})`); } if (errors.length) { pass = false; console.log('页面错误:', errors.slice(0, 2).join(' | ')); } console.log(`\n${pass ? '✓ 全部通过' : '✗ 存在失败项'}`); await browser.close(); process.exit(pass ? 0 : 1);