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

test("An unclosed bracket preserves the selected file chip", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("demo", { "src/app.ts": "export {};\n" });
  const thread = await t3.thread({ project, title: "File mention bracket proof", messages: ["Hello"] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  const counts: number[] = [];
  for (const [label, prefix] of [
    ["same-line-bracket", "See [WIP and "],
    ["control-no-bracket", "See WIP and "],
    ["preceding-line-bracket", "TODO: [draft\n"],
  ]) {
    await composer.tap();
    await composer.press("ControlOrMeta+a");
    await composer.press("Backspace");
    const lines = prefix.split("\n");
    await composer.pressSequentially(lines[0]);
    if (lines.length > 1) await composer.press("Shift+Enter");
    await composer.pressSequentially("@app.ts");
    await screen.getByRole("option", "app.ts", { exact: false }).waitFor();
    await composer.press("Enter");
    await composer.pressSequentially(" please");
    await app.screenshot(label);
    counts.push(await browser.evaluate(() => document.querySelectorAll('[data-composer-mention-chip]').length));
  }
  expect(counts).toEqual([1, 1, 1]);
});
