import { expect } from "e2e";
import { test, IOS } from "./t3-mobile.ts";
import { mobileProjectUpdate } from "./pr-mobile-seed.ts";

test("Mobile PR filter refreshes pins and keeps archives last", IOS, async ({t3, app, screen, device}) => {
  const alpha = await t3.project("alpha-old");
  const beta = await t3.project("beta-new");
  await t3.project("gamma-archived");
  await t3.thread({project:alpha, title:"Alpha earlier work", messages:["hello"]});
  await mobileProjectUpdate("alpha-old", {pinned:true});
  await t3.thread({project:beta, title:"Beta newer activity", messages:["hello"]});
  await mobileProjectUpdate("gamma-archived", {archived:true});
  await t3.open();
  await screen.getByText("PRs").tap();
  const ranks: boolean[] = [];
  const capture = async (label: string, pinned: boolean) => {
    await screen.getByLabel("More filters").tap();
    await screen.getByText("Project").tap();
    const a = await screen.getByText("alpha-old").boundingBox();
    const b = await screen.getByText("beta-new").boundingBox();
    const g = await screen.getByText("gamma-archived").boundingBox();
    await app.screenshot(label);
    ranks.push(Boolean(a && b && g && (pinned ? a.y < b.y : b.y < a.y) && g.y > a.y && g.y > b.y));
    await screen.getByText("All projects").tap();
  };
  await capture("mobile-pinned-and-archived", true);
  await mobileProjectUpdate("alpha-old", {pinned:false});
  await t3.thread({project:beta, title:"Beta after unpin", messages:["hello"]});
  await capture("mobile-after-unpin", false);
  await mobileProjectUpdate("alpha-old", {pinned:true});
  await t3.thread({project:beta, title:"Beta after repin", messages:["hello"]});
  await capture("mobile-after-repin", true);
  await expect(ranks).toEqual([true, true, true]);
});
