import { test, expect, handleElementDialogs } from "../fixtures/persistent-context";
import { assertNoDecryptionErrors } from "../fixtures/matrix-room";

test.describe("Login y sesion E2EE", () => {
  test("sesion cargada — rooms visibles en sidebar", async ({ page }) => {
    await page.goto("/");
    await handleElementDialogs(page);

    // Si llegamos aqui, handleElementDialogs ya verifico rooms sidebar
    const rooms = page.locator('[role="treeitem"], .mx_RoomTile');
    const roomCount = await rooms.count();
    expect(roomCount).toBeGreaterThan(0);
  });

  test("no hay mensajes Unable to decrypt en rooms recientes", async ({
    page,
  }) => {
    await page.goto("/");
    await handleElementDialogs(page);

    // Abrir el primer room visible para verificar mensajes
    const firstRoom = page.locator('[role="treeitem"], .mx_RoomTile').first();
    const roomCount = await firstRoom.count();

    if (roomCount > 0) {
      await firstRoom.click();
      await page.waitForTimeout(3_000);
      await assertNoDecryptionErrors(page);
    }
  });

  test("helpers de room navegan correctamente", async ({ page }) => {
    await page.goto("/");
    await handleElementDialogs(page);

    const rooms = page.locator('[role="treeitem"], .mx_RoomTile');
    const roomCount = await rooms.count();
    expect(roomCount).toBeGreaterThan(0);
  });
});
