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

test("Modified Backspace removes an empty list item in one press", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Empty list item", messages: ["Hello"] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  const results = [];
  for (const [kind, input] of [["ordered", "1. a"], ["bullet", "- a"], ["task", "- [ ] a"]]) {
    await composer.tap();
    await composer.press("Control+a");
    await composer.press("Backspace");
    await composer.pressSequentially(input!);
    await composer.press("Shift+Enter");
    await app.screenshot(`${kind}-empty-item`);
    await composer.press("Control+Backspace");
    await app.screenshot(`${kind}-after-one-modified-backspace`);
    results.push(await browser.evaluate(() => {
      const editor = document.querySelector('[role="textbox"]')!;
      return { items: editor.querySelectorAll("li").length, text: [...editor.querySelectorAll("li p")].map(p => p.textContent).join("|") };
    }));
  }
  expect(results).toEqual([{ items: 1, text: "a" }, { items: 1, text: "a" }, { items: 1, text: "a" }]);
});
