// Early Size Test - Verifies GetClientSize() returns reasonable values before Show()
// This reproduces KiCad's pattern where GetClientSize() is called in the constructor.
//
// Determinism: no waitForTimeout. Readiness via waitForWxApp (loud). The app emits its
// early client/frame size logs and a terminal PASS/FAIL during init; instead of sleeping
// 500ms we poll for the terminal PASS/FAIL marker, which guarantees every size log the
// assertions parse is already present. Static loaded/result states use stableShot.
import { test, expect, waitForWxApp } from './utils/fixtures';
import { stableShot } from './utils/element-tracker';

test.describe('Early GetClientSize() Tests', () => {

  test('Early size test app loads successfully', async ({ page, testLogger }) => {
    await page.goto('/standalone/earlysize/earlysize_test.html');
    await waitForWxApp(page);

    await stableShot(page, 'earlysize-01-loaded.png', { fullPage: true });

    const hasStartup = testLogger.consoleLogs.some(l => l.includes('[EARLYSIZE_TEST] Early size test app started'));

    expect(hasStartup, 'Startup log should be present').toBe(true);
    expect(testLogger.errors.filter(e => !e.includes('favicon'))).toHaveLength(0);
  });

  test('GetClientSize() returns reasonable values before Show()', async ({ page, testLogger }) => {
    await page.goto('/standalone/earlysize/earlysize_test.html');
    await waitForWxApp(page);

    // Wait for the app to finish initialization: it logs a terminal PASS/FAIL result once
    // its early size checks are done, which implies the client/frame size logs are present.
    await expect
      .poll(
        () =>
          testLogger.consoleLogs.some(
            l => l.includes('[EARLYSIZE_TEST] PASS') || l.includes('[EARLYSIZE_TEST] FAIL'),
          ),
        { message: 'earlysize test should log its terminal PASS/FAIL result' },
      )
      .toBe(true);

    await stableShot(page, 'earlysize-02-result.png', { fullPage: true });

    // Check for early client size log
    const clientSizeLogs = testLogger.consoleLogs.filter(l => l.includes('[EARLYSIZE_TEST] Early client size:'));
    expect(clientSizeLogs.length).toBeGreaterThan(0);

    // Parse the early client size
    const clientSizeLog = clientSizeLogs[0];
    const clientMatch = clientSizeLog.match(/Early client size: (\d+)x(\d+)/);
    expect(clientMatch, 'Client size log should contain dimensions').not.toBeNull();

    if (clientMatch) {
      const clientWidth = parseInt(clientMatch[1]);
      const clientHeight = parseInt(clientMatch[2]);

      // The key assertion: early client size should NOT be 20x20 or similar tiny values
      // This is the bug we're testing for - KiCad gets 20x20 here
      expect(clientWidth, `Early client width should be > 100 (got ${clientWidth})`).toBeGreaterThan(100);
      expect(clientHeight, `Early client height should be > 100 (got ${clientHeight})`).toBeGreaterThan(100);
    }

    // Check for early frame size log
    const frameSizeLogs = testLogger.consoleLogs.filter(l => l.includes('[EARLYSIZE_TEST] Early frame size:'));
    expect(frameSizeLogs.length).toBeGreaterThan(0);

    // Parse the early frame size
    const frameSizeLog = frameSizeLogs[0];
    const frameMatch = frameSizeLog.match(/Early frame size: (\d+)x(\d+)/);
    expect(frameMatch, 'Frame size log should contain dimensions').not.toBeNull();

    if (frameMatch) {
      const frameWidth = parseInt(frameMatch[1]);
      const frameHeight = parseInt(frameMatch[2]);

      // Frame size should also be reasonable
      expect(frameWidth, `Early frame width should be > 100 (got ${frameWidth})`).toBeGreaterThan(100);
      expect(frameHeight, `Early frame height should be > 100 (got ${frameHeight})`).toBeGreaterThan(100);
    }

    // Check for PASS/FAIL result
    const passLog = testLogger.consoleLogs.some(l => l.includes('[EARLYSIZE_TEST] PASS'));
    const failLog = testLogger.consoleLogs.some(l => l.includes('[EARLYSIZE_TEST] FAIL'));

    expect(failLog, 'Should not have FAIL log').toBe(false);
    expect(passLog, 'Should have PASS log').toBe(true);
  });
});
