const { chromium } = require('playwright');
const { join } = require('path');

(async () => {
  console.log('启动浏览器...');
  const browser = await chromium.launch({ headless: true });
  const page = await browser.newPage();

  // 设置视口大小
  await page.setViewportSize({ width: 1920, height: 1080 });

  console.log('导航到 http://localhost:8080 ...');
  try {
    await page.goto('http://localhost:8080', { waitUntil: 'networkidle', timeout: 10000 });
  } catch (error) {
    console.error('无法连接到 http://localhost:8080');
    console.error('请先启动开发服务器: pnpm dev');
    await browser.close();
    process.exit(1);
  }

  // 等待页面内容加载
  await page.waitForSelector('header', { timeout: 5000 });
  await page.waitForTimeout(1000); // 额外等待确保渲染完成

  // 截取全屏
  const screenshotPath = join(process.cwd(), 'homepage-full-screenshot.png');
  console.log('正在截取全屏...');
  await page.screenshot({
    path: screenshotPath,
    fullPage: true,
    animations: 'disabled' // 禁用动画以获得更清晰的截图
  });

  console.log(`截图已保存: ${screenshotPath}`);
  const fs = require('fs');
  console.log('文件大小:', fs.statSync(screenshotPath).size, 'bytes');

  await browser.close();
  console.log('完成！');
})();