/// <reference lib="dom" />
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import { runPromise, test, WEB } from "./t3.ts";

test("Unsent review comment details preserve tags and line breaks", WEB, async ({ t3, app, browser, screen }) => {
  await browser.setViewport({ width: 1440, height: 1000 });
  const project = await t3.project("review-demo", { "App.tsx": 'export function App() {\n  return <main>Hello</main>;\n}\n' });
  const thread = await t3.thread({ project, title: "Review App.tsx", messages: ["Start"] });
  await runPromise(Effect.flatMap(FileSystem.FileSystem, fs => fs.writeFileString(`${project}/App.tsx`, 'export function App() {\n  return <div>Hello</div>;\n}\n')));
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Diff", { exact: true }).tap();
  await screen.getByRole("button", "Expand App.tsx").tap();
  const point = (selector: string) => browser.evaluate(selector => {
    const element = document.querySelector("diffs-container")?.shadowRoot?.querySelector(selector);
    const box = element?.getBoundingClientRect();
    return box?.width && box.height ? { x: box.x + box.width / 2, y: box.y + box.height / 2 } : null;
  }, selector);
  await expect.poll(() => point('[data-column-number="2"][data-line-type="change-addition"]')).not.toBeNull();
  const line = await point('[data-column-number="2"][data-line-type="change-addition"]');
  if (!line) throw new Error("Added line unavailable");
  await browser.mouse.move(line.x, line.y);
  await expect.poll(async () => {
    const current = await point('[data-column-number="2"][data-line-type="change-addition"]');
    if (current) await browser.mouse.move(current.x, current.y);
    return point("[data-utility-button]");
  }).not.toBeNull();
  const control = await point("[data-utility-button]");
  if (!control) throw new Error("Comment control unavailable");
  await browser.mouse.move(control.x, control.y);
  await browser.mouse.down();
  await browser.mouse.up();
  await browser.locator('textarea[aria-label^="Comment on lines"]').focus();
  await browser.keyboard.type("Wrap this in <Suspense> instead");
  await browser.keyboard.press("Shift+Enter");
  await browser.keyboard.type("and drop the <div> wrapper");
  await screen.getByRole("button", "Comment", { exact: true }).tap();
  await screen.getByRole("button", "Review comment, App.tsx L2. Show details").tap();
  await expect.poll(() => browser.evaluate(() =>
    document.querySelector('[data-slot="popover-popup"] diffs-container')?.shadowRoot?.querySelector('pre')?.textContent?.includes('return') ?? false
  )).toBe(true);
  await app.screenshot("unsent-review-comment-details");
  const details = await browser.evaluate(() => {
    const popup = document.querySelector('[data-slot="popover-popup"]');
    const paragraph = Array.from(popup?.querySelectorAll("p") ?? []).find(p => p.textContent?.startsWith("Wrap this"));
    return { text: paragraph?.textContent, lineBreaks: paragraph?.querySelectorAll("br").length };
  });
  expect(details.text).toBe("Wrap this in <Suspense> instead\nand drop the <div> wrapper");
  expect(details.lineBreaks).toBe(1);
});
