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

test("Browser device preferences are read-only at project and checkout scope", WEB, async ({ t3, app, screen, browser }) => {
  const gitConfig = '[remote "origin"]\n  url = https://example.com/browser-proof.git\n  fetch = +refs/heads/*:refs/remotes/origin/*\n';
  await t3.project("Browser proof", { ".git/config": gitConfig });
  await t3.project("Browser checkout", { ".git/config": gitConfig });
  await t3.open("/settings/browser");
  // Hide synthetic filesystem locations in menu recordings; controls remain unchanged.
  await browser.evaluate(() => {
    const style = document.createElement("style");
    style.textContent = '[role="menuitemradio"] .text-xs { visibility: hidden; }';
    document.head.append(style);
  });
  await screen.getByRole("button", "Project scope: All projects").tap();
  await screen.getByRole("menuitemradio", "/example.com/browser-proof").tap();
  const results: boolean[] = [];
  const accessEditable: boolean[] = [];
  for (const scope of ["project", "checkout"]) {
    if (scope === "checkout") {
      await screen.getByRole("button", "Checkout scope: All checkouts").tap();
      await screen.getByRole("menuitemradio").filter({ hasText: "Browser checkout" }).tap();
    }
    const access = screen.getByRole("switch", "Agent browser access");
    accessEditable.push(await access.isEnabled());
    await access.tap();
    // Inert removes descendants from the accessibility tree; inspect this DOM state directly.
    const readOnly = await browser.evaluate(() => !!document.querySelector('[aria-label="Search engine"]')?.closest('[inert]'));
    results.push(readOnly);
    if (!readOnly) {
      await screen.getByRole("combobox", "Search engine").tap();
      await screen.getByRole("option", "Google", { exact: true }).tap();
      await screen.getByText("Setting not saved", { exact: true }).waitFor({ state: "visible" });
    } else {
      await browser.locator('span[tabindex="0"]:has([aria-label="Search engine"])').hover();
      await screen.getByText("Saved on this device. Select All projects to change it.", { exact: true }).waitFor({ state: "visible" });
    }
    await browser.evaluate(async () => {
      await Promise.all(document.getAnimations().filter(a => a.effect?.getTiming().iterations !== Infinity).map(a => a.finished.catch(() => {})));
    });
    await app.screenshot(`${scope}-search-engine`);
    if (!readOnly) await screen.getByRole("button", "Dismiss notification").tap();
  }
  await screen.getByRole("button", "Project scope: /example.com/browser-proof").tap();
  await screen.getByRole("menuitemradio", "All projects", { exact: true }).tap();
  await screen.getByRole("combobox", "Search engine").tap();
  await screen.getByRole("option", "Google", { exact: true }).tap();
  await expect(screen.getByRole("combobox", "Search engine")).toHaveText("Google");
  await app.screenshot("all-projects-editable");
  expect(accessEditable).toEqual([true, true]);
  expect(results).toEqual([true, true]);
});
