import { test, expect, _electron as electron } from '@playwright/test';
import { resolve } from 'node:path';

test('inbox shell shows v0.2 empty state', async () => {
  // Strip ELECTRON_RUN_AS_NODE if it leaked in from the parent process
  // (some harnesses set it for native-module rebuild and forget to clear it).
  // When set, Electron's main-process module hook is skipped and require('electron')
  // returns only the binary path, so app.whenReady is undefined.
  const env: Record<string, string> = {};
  for (const [k, v] of Object.entries(process.env)) {
    if (k === 'ELECTRON_RUN_AS_NODE') continue;
    if (typeof v === 'string') env[k] = v;
  }
  env.INKLING_DEBUG = '1';
  const app = await electron.launch({
    args: [resolve('out/main/index.js')],
    env
  });
  const first = await app.firstWindow();
  // Both the inbox and quickcapture windows are created at startup;
  // firstWindow() may pick either, so select by title.
  await new Promise((r) => setTimeout(r, 500));
  let inbox = first;
  for (const w of app.windows()) {
    if ((await w.title()) === 'Inkling') { inbox = w; break; }
  }
  await inbox.waitForLoadState('load');
  // v0.2.9 Cut B: 첫 launch 시 OnboardingWizard 표시 — "나중에 설정" 으로 dismiss 후 inbox 진입.
  const dismissOnboarding = inbox.getByRole('button', { name: /나중에 설정/ });
  if (await dismissOnboarding.isVisible({ timeout: 2000 }).catch(() => false)) {
    await dismissOnboarding.click();
  }
  await expect(inbox.getByRole('heading', { name: 'Inkling' })).toBeVisible();
  await expect(inbox.getByText('머릿속에 떠다니는 한 줄을 적어보세요.')).toBeVisible();
  await app.close();
});
