import { execFileSync } from "node:child_process";
import { expect } from "e2e";
import { IOS, test } from "./t3-mobile.ts";

test("The mobile author sheet selects and replaces a typed author", IOS, async ({ t3, app, screen, device }) => {
  const project = await t3.project("T3 Code");
  execFileSync("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project });
  await t3.open();
  await screen.getByRole("tab", "Pull requests", { exact: true }).tap();
  await screen.getByRole("button", "Search pull requests", { exact: true }).tap();
  const query = screen.getByPlaceholder("Search pull requests", { exact: true });
  await query.fill("author:StiensWout fix");
  await device.dismissKeyboard();
  const sheet = async () => {
    await screen.getByRole("button", "Filter by author", { exact: true }).tap();
    await screen.getByText("Search authors…", { exact: true }).tap();
    await screen.getByPlaceholder("Search authors", { exact: true }).waitFor();
  };
  await sheet();
  await screen.getByRole("radio", "StiensWout", { exact: true }).waitFor();
  const selected = await screen.getByRole("radio", "StiensWout", { exact: true }).isChecked();
  await app.screenshot("mobile-typed-author-selected");
  await screen.getByPlaceholder("Search authors", { exact: true }).fill("saphid");
  await screen.getByRole("radio", "saphid", { exact: true }).tap();
  await query.waitFor();
  await device.dismissKeyboard();
  await app.screenshot("mobile-author-replaced");
  const replaced = await query.inputValue();
  await query.fill("author:saphid fix");
  await device.dismissKeyboard();
  await sheet();
  await screen.getByRole("radio", "saphid", { exact: true }).tap();
  await query.waitFor();
  await app.screenshot("mobile-author-cleared");
  const cleared = await query.inputValue();
  expect({ selected, replaced, cleared }).toEqual({ selected: true, replaced: "fix", cleared: "fix" });
});
