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

test("Usage closes to the draft without reloading after pairing", WEB, async ({ t3, app, browser, screen }) => {
  await t3.project("Usage close demo");
  await t3.open();
  await screen.getByRole("button", "Usage", { exact: true }).tap();
  await screen.getByRole("button", "Close", { exact: true }).tap();
  await screen.getByRole("button", "New thread", { exact: true }).tap();
  const draftPath = await browser.evaluate(() => location.pathname);
  await browser.evaluate(() => { document.documentElement.dataset.usageCloseMarker = "same-document"; });
  await screen.getByRole("button", "Usage", { exact: true }).tap();
  await app.screenshot("usage-before-close");
  await screen.getByRole("button", "Close", { exact: true }).tap();
  // Let the modal transition and any document navigation finish before capturing the outcome.
  await new Promise((resolve) => setTimeout(resolve, 2500));
  await app.screenshot("usage-after-close");
  expect(await browser.evaluate(() => ({ path: location.pathname, marker: document.documentElement.dataset.usageCloseMarker ?? null }))).toEqual({ path: draftPath, marker: "same-document" });
});
