// apps/client/test/e2e/logout.e2e.test.ts
// [int->REQ-CLI-02] [int->REQ-CLI-03]
// Plan 06-11 — Playwright e2e for Esc menu logout flow (D-24 + D-34).
//
// Tests:
//   1. Esc opens the menu; Logout returns to LoginScene with cleared session
//      cookies — cookie assertion uses page.context().cookies() (Better-Auth
//      sets HttpOnly so document.cookie is always empty; page.context().cookies()
//      sees the full cookie jar). Shape is toHaveLength(0), NOT
//      every(c => !c.value) which passes spuriously when cookies are absent.
//   2. Canvas pointerdown opens the menu (D-24 secondary trigger).
//   3. Resume closes the menu without navigating away.

import { test, expect, loginAs, waitForGameReady } from './fixtures.js';

test.describe('Esc menu', () => {
  // Test 1: Esc → menu visible → Logout → LoginScene + cleared session cookies
  test('Esc opens menu; Logout returns to LoginScene with cleared session cookies (D-34)', async ({
    page,
    accountA,
    inviteSuffix,
  }) => {
    await loginAs(page, accountA, inviteSuffix);
    await waitForGameReady(page);

    // Open the Esc menu via keyboard
    await page.keyboard.press('Escape');

    // Menu should be visible
    await expect(page.locator('[data-testid="esc-menu"]')).toBeVisible({
      timeout: 5_000,
    });

    // Click Logout
    await page.locator('[data-testid="esc-menu-logout"]').click();

    // Should navigate back to LoginScene (login form heading visible)
    await expect(page.locator('form#login-form, #login-form, #username')).toBeVisible({
      timeout: 5_000,
    });

    // D-34 cookie assertion: use page.context().cookies() NOT document.cookie
    // (Better-Auth is HttpOnly so document.cookie would be empty regardless).
    // Assert session-cookie count is zero — toHaveLength(0), NOT every(c => !c.value).
    const cookies = await page.context().cookies();
    const sessionCookies = cookies.filter((c) => /better-auth|session/i.test(c.name));
    expect(sessionCookies).toHaveLength(0);
  });

  // Test 2: canvas pointerdown opens menu (D-24 secondary trigger)
  test('canvas click opens menu (D-24 secondary trigger)', async ({
    page,
    accountA,
    inviteSuffix,
  }) => {
    await loginAs(page, accountA, inviteSuffix);
    await waitForGameReady(page);

    // Click the canvas (not chat area — use coordinates away from any UI)
    await page.locator('canvas[data-game-ready="true"]').click({ position: { x: 100, y: 100 } });

    // Menu should be visible
    await expect(page.locator('[data-testid="esc-menu"]')).toBeVisible({
      timeout: 5_000,
    });
  });

  // Test 3: Resume closes the menu and does not navigate away
  test('Resume closes menu and re-engages movement without navigating', async ({
    page,
    accountA,
    inviteSuffix,
  }) => {
    await loginAs(page, accountA, inviteSuffix);
    await waitForGameReady(page);

    // Open via Escape
    await page.keyboard.press('Escape');
    await expect(page.locator('[data-testid="esc-menu"]')).toBeVisible({
      timeout: 5_000,
    });

    // Click Resume
    await page.locator('[data-testid="esc-menu-resume"]').click();

    // Menu should be hidden
    await expect(page.locator('[data-testid="esc-menu"]')).toBeHidden({
      timeout: 3_000,
    });

    // Should still be in GameScene (canvas still present)
    await expect(page.locator('canvas[data-game-ready="true"]')).toBeVisible();
  });
});
