// 诊断 v2：打印完整 CK_STACK（定位 framework 抛的错误的源头）
const { chromium } = require('playwright');
const BASE = 'http://127.0.0.1:8099';
const ROUTES = ['/buy-service/1/pos', '/download'];

(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.stack || e.message}`));
    page.on('console', m => allLogs.push(m.text()));
    await page.goto(BASE + route, { waitUntil: 'load', timeout: 30000 }).catch(()=>{});
    await page.waitForTimeout(5000);

    console.log(`\n########## ${route} ##########`);
    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, 150));
        if (allLogs[i+1] && allLogs[i+1].startsWith('CK_STACK:')) {
          // 打印堆栈里所有 flutter_wisetronic 帧
          const proj = allLogs[i+1].match(/flutter_wisetronic\/[^\s]+/g) || [];
          console.log('   项目帧: ' + (proj.length ? [...new Set(proj)].join('\n            ') : '(无)'));
        }
      }
    }
    await ctx.close();
  }
  await browser.close();
})();
