/** 验证:按屏幕宽度自动进入移动端/桌面端,不再手动切换 */ import { chromium } from 'playwright'; import { mkdirSync } from 'node:fs'; import { resolve } from 'node:path'; import { loadDevLogin } from './dev-login.mjs'; const OUT = resolve('test-results/fix-20261110'); mkdirSync(OUT, { recursive: true }); const login = loadDevLogin(); const b = await chromium.launch({ headless: true, channel: 'chromium' }); async function testAt(name, viewport) { const ctx = await b.newContext({ viewport, isMobile: viewport.width < 768, hasTouch: viewport.width < 768 }); const p = await ctx.newPage(); await p.addInitScript((d) => { localStorage.setItem('fmode_session_token', d.token); localStorage.setItem('fmode_me', JSON.stringify(d.me)); }, login.payload); // 访问根路径,看自动分流 await p.goto('http://localhost:4200/', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(5000); const url = p.url(); const expect = viewport.width < 768 ? '/mobile/' : '/desktop/'; const ok = url.includes(expect); console.log(`${name} (${viewport.width}px) → ${url.replace('http://localhost:4200', '')} ${ok ? '✅' : '❌ 应为 ' + expect}`); await p.screenshot({ path: `${OUT}/90-${name}.png` }); await ctx.close(); return ok; } const r1 = await testAt('phone', { width: 390, height: 844 }); const r2 = await testAt('tablet', { width: 1024, height: 768 }); const r3 = await testAt('desktop', { width: 1440, height: 900 }); const r4 = await testAt('narrow-window', { width: 600, height: 900 }); console.log('\n全部通过:', r1 && r2 && r3 && r4 ? '✅' : '❌'); await b.close();