/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("A paragraph after an ordered list stays outside the sent list", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("List paragraph demo");
  const thread = await t3.thread({ project, title: "List paragraph", messages: ["Start"], model: "showcase" });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.tap();
  await browser.keyboard.type("1. a");
  await browser.keyboard.press("Shift+Enter");
  await browser.keyboard.type("b");
  await browser.keyboard.press("Shift+Enter");
  await browser.keyboard.type("c");
  await browser.keyboard.press("Shift+Enter");
  await browser.keyboard.press("Shift+Enter");
  await browser.keyboard.type("d");
  await app.screenshot("composer");
  await composer.press("Enter");
  await expect(browser.locator('[data-user-message-body]').last()).toContainText("d");
  await expect(screen.getByRole("button", "Stop generation")).toBeHidden({ timeout: 60_000 });
  await browser.evaluate(() => {
    [...document.querySelectorAll("[data-user-message-body]")].at(-1)?.scrollIntoView({ block: "center" });
  });
  await app.screenshot("sent");
  const outside = await browser.evaluate(() => {
    const body = [...document.querySelectorAll('[data-user-message-body]')].at(-1);
    return [...(body?.querySelectorAll('p') ?? [])].some(p => p.textContent === 'd' && !p.closest('li'));
  });
  expect(outside).toBe(true);
});
