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

test("Find highlights the complete CRLF quoted comment", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("quote-find");
  const thread = await t3.thread({ project, title: "CRLF quoted comment", messages: ["Start"], model: "showcase" });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.fill("CRLF check [Assistant quote](t3-citation://v1/a/b/c?text=quote&start=0&end=5&prefix=&suffix=&comment=first%0D%0Aneedle)");
  await composer.press("Enter");
  await browser.locator('[data-user-message-body]').filter({ hasText: "needle" }).waitFor();
  await browser.keyboard.press("Control+f");
  const find = screen.getByRole("searchbox", "Find in thread");
  const ranges: unknown[] = [];
  for (const [query, label] of [["needle", "needle-highlight"], ["first needle", "whole-comment-highlight"]]) {
    await find.fill(query);
    await browser.locator('[data-thread-find-bar] span[aria-hidden]').filter({ hasText: "1/1" }).waitFor();
    await expect.poll(() => browser.evaluate(() => CSS.highlights.get("t3-thread-find-active")?.size ?? 0)).toBe(1);
    await app.screenshot(label);
    ranges.push(await browser.evaluate(() => Array.from(CSS.highlights.get("t3-thread-find-active") ?? [], r => ({ text: r.toString(), start: r.startOffset, end: r.endOffset, node: r.startContainer.textContent }))));
  }
  expect(ranges).toEqual([
    [{ text: "needle", start: 7, end: 13, node: "first\r\nneedle" }],
    [{ text: "first\r\nneedle", start: 0, end: 13, node: "first\r\nneedle" }],
  ]);
});
