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

const REPOSITORY = "julius54/pr44591-diff-order-repro-deletion_scheduled-89574930";

test("An explicitly imported changed module precedes its importer", WEB, async ({ t3, app, browser, screen }) => {
  await t3.project("Module ordering", {
    ".git/config": `[remote "origin"]\nurl = https://gitlab.com/${REPOSITORY}.git\n`,
  });
  await t3.open(`/pull-requests?${new URLSearchParams({ repository: REPOSITORY, number: "3", host: "gitlab.com" })}`);
  await screen.getByRole("button", "Code", { exact: true }).tap();
  const order = () => browser.evaluate(() => {
    const names: string[] = [];
    for (const host of document.querySelectorAll("*")) {
      for (const title of host.shadowRoot?.querySelectorAll("[data-diffs-header] [data-title]") ?? []) {
        const name = title.textContent?.trim();
        if (name && ["a.js", "z.cjs", "z.js", "zz.js"].includes(name)) names.push(name);
      }
    }
    return names;
  });
  await screen.getByRole("button", "Expand all files").waitFor();
  await screen.getByRole("button", /^Open #3 Add cjs module with a dependency/).waitFor();
  await app.screenshot("module-review-order");
  await expect.poll(order).toEqual(["z.js", "zz.js", "z.cjs", "a.js"]);
});
