import { test, expect } from "@playwright/test";
import {
  goToRoom,
  sendMessage,
  waitForBotReply,
  assertNoDecryptionErrors,
} from "../fixtures/matrix-room";

test.describe("asistente-2", () => {
  test.beforeEach(async ({ page }) => {
    await page.goto("/");

    // Esperar a que la sesion este lista
    await expect(
      page.locator('[role="tree"][aria-label="Rooms"]')
    ).toBeVisible({ timeout: 30_000 });

    await goToRoom(page, "Asistente 2");
  });

  test("responde a un saludo", async ({ page }) => {
    await sendMessage(page, "Hola, que tal?");

    const reply = await waitForBotReply(page, {
      timeout: 60_000,
      sender: "Asistente 2",
    });
    expect(reply).toBeTruthy();
    expect(reply.length).toBeGreaterThan(10);
  });

  test("!tools muestra herramientas disponibles", async ({ page }) => {
    await sendMessage(page, "!tools");

    const reply = await waitForBotReply(page, {
      timeout: 10_000,
      sender: "Asistente 2",
    });
    expect(reply).toBeTruthy();
    // asistente-2 tiene al menos current_time
    expect(reply.toLowerCase()).toMatch(/current_time|hora|herramienta|tool/);
  });

  test("pregunta que activa tool use (que hora es?)", async ({ page }) => {
    await sendMessage(page, "Que hora es ahora mismo?");

    const reply = await waitForBotReply(page, {
      timeout: 60_000,
      sender: "Asistente 2",
    });
    expect(reply).toBeTruthy();
    // La respuesta debe contener algo relacionado con tiempo/hora
    expect(reply.length).toBeGreaterThan(5);
  });

  test("!help muestra comandos", async ({ page }) => {
    await sendMessage(page, "!help");

    const reply = await waitForBotReply(page, {
      timeout: 10_000,
      sender: "Asistente 2",
    });
    expect(reply).toBeTruthy();
    expect(reply.toLowerCase()).toContain("help");
    expect(reply.toLowerCase()).toContain("ping");
  });

  test("no hay errores de E2EE en el timeline", async ({ page }) => {
    await assertNoDecryptionErrors(page);
  });
});
