import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Successive project setting saves preserve both changes", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("Rapid saves demo");
  await t3.open("/settings/source-control");
  await screen.getByRole("button", "Project scope: All projects").tap();
  await screen.getByRole("menuitemradio", "Rapid saves demo").tap();
  const pull = screen.getByRole("switch", "Default automatic pull");
  const credits = screen.getByRole("switch", "Remove agent credits when merging");
  // Both defaults are off. Use the reported same-browser-task reproduction.
  await pull.scrollIntoView();
  await new Promise((resolve) => setTimeout(resolve, 5000));
  await app.screenshot("both-off");
  await browser.evaluate(() => {
    const pull = document.querySelector<HTMLElement>('[role="switch"][aria-label="Default automatic pull"]')!;
    const credits = document.querySelector<HTMLElement>('[role="switch"][aria-label="Remove agent credits when merging"]')!;
    pull.click();
    credits.click();
  });
  await new Promise((resolve) => setTimeout(resolve, 5000));
  await app.screenshot("rapid-saves-settled");
  const url = await browser.evaluate(() => location.href);
  await app.open(url);
  await pull.scrollIntoView();
  await new Promise((resolve) => setTimeout(resolve, 5000));
  await app.screenshot("persisted-after-reload");
  await expect(pull).toHaveAttribute("aria-checked", "true");
  await expect(credits).toHaveAttribute("aria-checked", "true");
});
