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

test("Archiving the open alpha thread keeps the new draft in alpha", WEB, async ({ t3, app, browser, screen }) => {
  const alpha = await t3.project("alpha");
  const old = await t3.thread({ project: alpha, title: "Old alpha thread", messages: ["First alpha message"] });
  const beta = await t3.project("beta");
  await t3.thread({ project: beta, title: "Recent beta thread", messages: ["Newer beta message"] });
  await t3.open(old);
  await screen.getByText("Old alpha thread", { exact: true }).first().waitFor();
  await app.screenshot("open-alpha-before-archive");
  await browser.evaluate(() => {
    const title = [...document.querySelectorAll("*")].find(el => el.textContent === "Old alpha thread" && el.children.length === 0);
    if (!title) throw new Error("Old alpha thread row missing");
    const box = title.getBoundingClientRect();
    title.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true, cancelable: true, button: 2, clientX: box.x + box.width / 2, clientY: box.y + box.height / 2 }));
    return null;
  });
  await screen.getByRole("menuitem", "Archive thread", { exact: true }).tap();
  await screen.getByRole("heading", /What should we build/).waitFor();
  await browser.evaluate(async () => { await new Promise(resolve => setTimeout(resolve, 1500)); return null; });
  await app.screenshot("draft-after-archiving-alpha");
  await expect(screen.getByRole("heading", /What should we build/)).toContainText("alpha");
});
