// 诊断：对失败路由打印完整错误 + CK_STACK
const { chromium } = require('playwright');
const BASE = 'http://127.0.0.1:8099';
const ROUTES = ['/contact-stores', '/download', '/buy-service/1/pos'];

(async () => {
  const browser = await chromium.launch({ headless: true });
  for (const route of ROUTES) {
    const ctx = await browser.newContext({ viewport: { width: 1280, height: 900 } });
    const page = await ctx.newPage();
    const allLogs = [];
    page.on('pageerror', e => allLogs.push(`PAGEERROR: ${e.message}`));
    page.on('console', m => allLogs.push(m.text()));
    await page.goto(BASE + route, { waitUntil: 'load', timeout: 30000 }).catch(e => allLogs.push('GOTO: ' + e.message.slice(0,80)));
    await page.waitForTimeout(5000);

    console.log(`\n########## ${route} ##########`);
    // 只打印 CK_ERROR + CK_STACK + PAGEERROR
    for (let i = 0; i < allLogs.length; i++) {
      const l = allLogs[i];
      if (l.startsWith('CK_ERROR:') || l.startsWith('PAGEERROR')) {
        console.log(l.slice(0, 300));
        // 找紧跟的 CK_STACK
        if (allLogs[i+1] && allLogs[i+1].startsWith('CK_STACK:')) {
          const proj = allLogs[i+1].match(/flutter_wisetronic\/[^\s]+:\d+/g);
          console.log('  → 项目位置:', proj ? [...new Set(proj)].slice(0,4) : '(无项目帧)');
        }
      }
    }
    await ctx.close();
  }
  await browser.close();
})();
