// apps/client/test/e2e/cli-08-tiles.e2e.test.ts
// [int->REQ-CLI-08]
//
// Plan 06.1-07 Task 1 — GREEN refinement of the 06.1-03 RED skeleton. Asserts
// the Wave 2 D-40 verify-bail flip (Plan 06.1-05) actually emits floor tiles
// + D6.1-25 TSide1 sides via the __rebno hooks RoomRenderer.renderNew exposes.
//
// Assertion model (per 06.1-PATTERNS.md S-08 + S-05):
//   - __rebno.tileCount: populated by RoomRenderer.renderNew (Plan 06.1-05)
//     after onRoomLayout fires. Polled (not single-shot read) because the
//     s2c.room_layout event may land a few hundred ms after onLocalSnapshot
//     sets data-game-ready=true; polling lets the test track the actual
//     room-render moment without a flaky fixed sleep.
//   - __rebno.loadedTextures: 'atlas-mvp' is loaded by atlas-loader.ts queue
//     during preload; serves as a regression guard against atlas-load drift.
//   - __rebno.tsideCount: TSide1 sprite count populated by
//     RoomRenderer.renderNew (Plan 06.1-05 D6.1-25 bottom-edge placement).

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

test('CLI-08 tiles — RoomRenderer emits floor tiles + TSide1 sides (Wave 4 GREEN gate)', async ({
  page,
  accountA,
  inviteSuffix,
}) => {
  await loginAs(page, accountA, inviteSuffix);
  await waitForGameReady(page);

  // 1. atlas-mvp must be loaded into the Phaser TextureManager (atlas-loader.ts
  //    queues it on scene preload). Polled to avoid race with the TextureManager
  //    finishing its onload promise mid-frame.
  await expect
    .poll(
      async () =>
        page.evaluate(
          () =>
            (
              window as unknown as {
                __rebno?: { loadedTextures?: string[] };
              }
            ).__rebno?.loadedTextures ?? [],
        ),
      { timeout: 10_000 },
    )
    .toContain('atlas-mvp');

  // 2. Floor tiles must render. The s2c.room_layout event may land slightly
  //    after onLocalSnapshot (which is what flips data-game-ready), so poll
  //    __rebno.tileCount instead of doing a single-shot read. Polling resolves
  //    once RoomRenderer.renderNew (Plan 06.1-05) has placed at least one tile
  //    via group.add(). mvp-room ships 324 tiles, so any positive count clears.
  await expect
    .poll(
      async () =>
        page.evaluate(
          () =>
            (
              window as unknown as {
                __rebno?: { tileCount?: number };
              }
            ).__rebno?.tileCount ?? 0,
        ),
      {
        message:
          'RoomRenderer.renderNew must emit floor tile sprites for mvp-room (D-40 verify-bail flip / Plan 06.1-05)',
        timeout: 15_000,
      },
    )
    .toBeGreaterThan(0);

  // 3. TSide1 bottom-edge sprite presence (D6.1-25). Same poll model — Plan
  //    06.1-05's RoomRenderer.renderNew placement loop is part of the same
  //    render() call, so by the time tileCount > 0 the tsideCount has also
  //    been published. Poll anyway to harden against ordering surprises.
  await expect
    .poll(
      async () =>
        page.evaluate(
          () =>
            (
              window as unknown as {
                __rebno?: { tsideCount?: number };
              }
            ).__rebno?.tsideCount ?? 0,
        ),
      {
        message:
          'RoomRenderer must emit 0024-TSide1 sprites along floor bottom edges (D6.1-25)',
        timeout: 10_000,
      },
    )
    .toBeGreaterThan(0);
});
