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

test("Tab closes the numbering gap after nesting an item", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Numbered list", messages: ["Hello"] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.fill("1. a\n2. b\n3. c");
  await composer.press("ControlOrMeta+End");
  await composer.press("ArrowUp");
  await app.screenshot("original-list");
  await composer.press("Tab");
  await app.screenshot("nested-second-item");
  // Hold the visible result so the recording captures the changed numbering.
  await browser.evaluate(() => new Promise<void>(resolve => setTimeout(resolve, 1500)));
  const nested = await browser.evaluate(() => [...document.querySelectorAll('[role="textbox"] ol > li')].map(item => `${item.getAttribute("data-marker")} ${item.querySelector("p")?.textContent}`));
  await composer.press("Shift+Tab");
  await app.screenshot("restored-list");
  await browser.evaluate(() => new Promise<void>(resolve => setTimeout(resolve, 1500)));
  expect(nested, "The following parent item closes the numbering gap").toEqual(["1. a", "1. b", "2. c"]);
  const restored = await browser.evaluate(() => [...document.querySelectorAll('[role="textbox"] ol > li')].map(item => `${item.getAttribute("data-marker")} ${item.querySelector("p")?.textContent}`));
  expect(restored, "Shift+Tab restores the original list").toEqual(["1. a", "2. b", "3. c"]);
});
