import puppeteer from 'puppeteer';

const browser = await puppeteer.launch({
  headless: true,
  args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();

console.log('1. 访问登录页面...');
await page.goto('http://localhost:5175/login', { waitUntil: 'networkidle0' });

console.log('2. 等待应用初始化...');
await page.waitForFunction(() => window.__ETHER_APP_INITIALIZED__ === true, { timeout: 30000 });
await page.waitForTimeout(1000);

console.log('3. 输入用户名密码...');
await page.type('#username', 'admin', { delay: 50 });
await page.type('#password', 'Admin@123', { delay: 50 });

console.log('4. 点击登录...');
await page.click('#login-button');

await page.waitForTimeout(3000);

console.log('5. 检查是否登录成功...');
const url = page.url();
console.log('当前URL:', url);

// 进入项目管理页面
console.log('6. 进入项目管理页面...');
await page.goto('http://localhost:5175/project', { waitUntil: 'networkidle0' });
await page.waitForTimeout(2000);

// 截图查看当前页面
await page.screenshot({ path: '/tmp/project-page.png' });
console.log('已截图: /tmp/project-page.png');

// 尝试找到空间管理入口
const menuTexts = await page.evaluate(() => {
  const items = document.querySelectorAll('li');
  return Array.from(items).map(i => i.textContent.trim()).slice(0, 20);
});
console.log('菜单文本:', menuTexts);

await browser.close();
console.log('测试完成');
