import { expect } from "e2e";

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

test(
  "Optional extras accept an empty selection for Next and Submit",
  WEB,
  async ({ t3, app, screen }) => {
    const project = await t3.project("Question demo");
    const last = await t3.thread({
      project,
      title: "Extras last",
      model: "question",
      messages: ["Choose language and extras"],
    });
    await t3.open(last);
    await screen.getByText("TypeScript", { exact: true }).tap();
    await screen.getByText("Lint", { exact: true }).waitFor();
    await app.screenshot("empty-extras-submit");
    await screen.getByText("Lint", { exact: true }).tap();
    await app.screenshot("lint-selected-submit");
    await screen.getByText("Lint", { exact: true }).tap();
    await app.screenshot("lint-cleared-submit");

    const first = await t3.thread({
      project,
      title: "Extras first",
      model: "todo-list",
      messages: ["Choose extras then language"],
    });
    await t3.open(first);
    await screen.getByText("Lint", { exact: true }).waitFor();
    await app.screenshot("empty-extras-next");
    await expect(screen.getByRole("button", "Next")).toBeEnabled();
    await screen.getByRole("button", "Next").tap();
    await screen.getByText("TypeScript", { exact: true }).waitFor();
    await app.screenshot("language-required");
    await expect(screen.getByRole("button", "Submit")).toBeDisabled();
    await screen.getByText("TypeScript", { exact: true }).tap();
    await screen.getByRole("button", "Submit").tap();
    await expect(screen.getByText("Empty extras accepted.", { exact: true })).toBeVisible();
    await app.screenshot("empty-extras-submitted");

    await t3.open(last);
    await screen.getByText("TypeScript", { exact: true }).tap();
    await screen.getByText("Lint", { exact: true }).waitFor();
    await expect(screen.getByRole("button", "Submit")).toBeEnabled();
    await screen.getByRole("button", "Submit").tap();
    await expect(screen.getByText("Empty extras accepted.", { exact: true })).toBeVisible();
    await app.screenshot("extras-last-submitted");
  },
);
