import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Message results preserve inner quote punctuation", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Quote search demo");
  const thread = await t3.thread({ project, title: "Run npm Command", messages: ["Start"] });
  await t3.open(thread);
  await browser.locator('[data-slot="sidebar-rail"]').waitFor();
  const rail = await browser.evaluate(() => {
    const box = document.querySelector('[data-slot="sidebar-rail"]')!.getBoundingClientRect();
    return { x: box.x + box.width / 2, y: box.y + box.height / 2 };
  });
  await browser.mouse.move(rail.x, rail.y);
  await browser.mouse.down();
  await browser.mouse.move(rail.x + 300, rail.y);
  await browser.mouse.up();
  const composer = screen.getByRole("textbox");
  await composer.pressSequentially("npm is not the command: run 'npm' now");
  await composer.press("Enter");
  await screen.getByText("second fixture turn complete", { exact: false }).waitFor();
  await screen.getByRole("combobox", "Search threads").fill("run 'npm' now");
  await screen.getByRole("listbox", "Thread search results").getByText("You:", { exact: false }).waitFor();
  await app.screenshot("sidebar-quote-highlights");
  const sidebarMarks = await browser.evaluate(() => Array.from(document.querySelectorAll('[role="listbox"] mark'), el => el.textContent));
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Control+k");
  const palette = screen.getByRole("dialog", "Command palette");
  await palette.getByRole("combobox").fill("run 'npm' now");
  await palette.getByText("You:", { exact: false }).waitFor();
  await app.screenshot("palette-quote-highlights");
  const paletteMarks = await browser.evaluate(() => Array.from(document.querySelectorAll('[role="dialog"] mark'), el => el.textContent));
  expect({ sidebarMarks, paletteMarks }).toEqual({ sidebarMarks: ["run", "'npm'", "now"], paletteMarks: ["run", "'npm'", "now"] });
});
