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

test("All projects exposes searchable editable default actions inherited by projects", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Example project");
  const thread = await t3.thread({ project, title: "Default action inheritance", messages: ["Start"] });
  await browser.setViewport({ width: 1440, height: 1000 });
  await browser.addInitScript(() => {
    const redact = () => {
      for (const el of document.querySelectorAll<HTMLElement>("span, code, p")) {
        if (el.childElementCount === 0 && /\/(home|tmp)\//.test(el.textContent ?? "")) {
          el.style.visibility = "hidden";
        }
      }
    };
    new MutationObserver(redact).observe(document, { childList: true, subtree: true });
  });
  await t3.open("/settings/projects");
  const search = screen.getByRole("combobox", "Search settings");
  await search.fill("Actions");
  await app.screenshot("all-projects-actions-search");
  await screen.getByRole("option").filter({ hasText: "Actions" }).tap();
  await app.screenshot("all-projects-search-destination");
  await screen.getByRole("button", "Environment scope: All environments").tap();
  await screen.getByRole("menuitemradio").filter({ hasText: "Scenario" }).tap();
  await search.fill("Actions");
  await app.screenshot("environment-actions-search");
  await screen.getByRole("option").filter({ hasText: "Actions" }).tap();
  await app.screenshot("environment-search-destination");
  await search.fill("");
  await app.screenshot("all-projects-actions-section");
  await expect(screen.getByRole("button", "Add action", { exact: true })).toBeVisible();
  await search.fill("Actions");
  await screen.getByRole("option").filter({ hasText: "Actions" }).tap();
  await screen.getByRole("button", "Add action", { exact: true }).tap();
  await browser.locator("#script-name").fill("Default check");
  await browser.locator("#script-command").fill("echo initial");
  await app.screenshot("add-machine-wide-action");
  await screen.getByRole("button", "Save action", { exact: true }).tap();
  await screen.getByText("echo initial", { exact: true }).waitFor();
  await screen.getByRole("button", "Edit Default check", { exact: true }).tap();
  await browser.locator("#script-command").fill("echo inherited-default");
  await app.screenshot("edit-machine-wide-action");
  await screen.getByRole("button", "Save changes", { exact: true }).tap();
  await screen.getByText("echo inherited-default", { exact: true }).waitFor();
  await t3.open("/settings/projects");
  await screen.getByText("echo inherited-default", { exact: true }).waitFor();
  await app.screenshot("saved-machine-wide-action");
  await screen.getByRole("button", "Project scope: All projects").tap();
  await screen.getByRole("menuitemradio").filter({ hasText: "Example project" }).tap();
  await screen.getByText("echo inherited-default", { exact: true }).waitFor();
  await screen.getByText("echo inherited-default", { exact: true }).scrollIntoView();
  await app.screenshot("project-inherits-default-action");
  await t3.open(thread);
  await screen.getByRole("button", "Run Default check", { exact: true }).waitFor();
  await screen.getByText(/Syncing messages/).waitFor({ state: "hidden" });
  await app.screenshot("inherited-action-in-thread");
  await expect(screen.getByRole("button", "Run Default check", { exact: true })).toBeVisible();
});
