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

test("Editing Theme JSON clears the previous import error and submitting validates again", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("demo");
  await t3.open("/settings/appearance");
  await screen.getByRole("button", "Add theme", { exact: true }).tap();
  const dialog = screen.getByRole("dialog", "Add a theme");
  const input = dialog.getByRole("textbox", "Theme JSON");
  const submit = dialog.getByRole("button", "Add theme", { exact: true });
  await input.fill("{");
  await submit.tap();
  await dialog.getByRole("alert").waitFor();
  await app.screenshot("invalid-json-submitted");
  await input.fill("{}");
  await app.screenshot("edited-valid-json");
  const editedErrors = await browser.evaluate(() => Array.from(document.querySelectorAll('[role="alert"]')).map(el => el.textContent));
  await input.fill("");
  await app.screenshot("cleared-json");
  const clearedErrors = await browser.evaluate(() => Array.from(document.querySelectorAll('[role="alert"]')).map(el => el.textContent));
  await input.fill("{");
  await submit.tap();
  await dialog.getByRole("alert").waitFor();
  await app.screenshot("resubmitted-invalid-json");
  const resubmitted = await browser.evaluate(() => document.querySelector('[role="alert"]')?.textContent);
  expect({ editedErrors, clearedErrors, resubmitted }).toEqual({ editedErrors: [], clearedErrors: [], resubmitted: "That theme file is not valid JSON." });
});
