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

test("Greek sigma prefixes find the original branch in both web pickers", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("greek-repo", {"t3.json": JSON.stringify({defaultThreadEnvMode:"local"})});
  const git = (...args: string[]) => runPromise(run("git", args, {cwd: project}));
  await git("branch", "feature/ΟΣΑ");
  await git("branch", "other");
  const thread = await t3.thread({project, title:"Greek branch search", messages:["Start"]});
  await t3.open(thread);
  await screen.getByRole("button", "Toggle thread details panel", {exact:true}).waitFor();
  const branch = browser.locator('[data-slot="combobox-trigger"][aria-label^="Branch:"]:visible').first();
  if (!(await branch.isVisible())) await screen.getByRole("button", "Toggle thread details panel", {exact:true}).tap();
  await branch.tap();
  await screen.getByRole("option", "other", {exact:true}).waitFor();
  const search = screen.getByPlaceholder("Search refs...");
  await search.fill("feature/οσ");
  await screen.getByRole("option", /^feature\/\s*ΟΣΑ$/, {exact:true}).waitFor();
  await app.screenshot("toolbar-lowercase-control");
  await search.fill("feature/ΟΣ");
  await app.screenshot("toolbar-uppercase-prefix");
  const toolbarFound = await screen.getByRole("option", /^feature\/\s*ΟΣΑ$/, {exact:true}).isVisible();
  if (toolbarFound) {
    await screen.getByRole("option", /^feature\/\s*ΟΣΑ$/, {exact:true}).tap();
    await branch.filter({hasText:/feature\/\s*ΟΣΑ/}).waitFor();
    await expect(branch).toBeEnabled();
  } else await browser.keyboard.press("Escape");
  const selected = toolbarFound ? (await git("branch", "--show-current")).stdout.trim() : "";
  if (toolbarFound) {
    await branch.tap();
    await screen.getByRole("option", "main", {exact:true}).tap();
    await branch.filter({hasText:"main"}).waitFor();
    await expect(branch).toBeEnabled();
  }
  await screen.getByRole("button", "Toggle right panel", {exact:true}).tap();
  await screen.getByText("Diff", {exact:true}).tap();
  await screen.getByRole("combobox", /^Change comparison target/).tap();
  await screen.getByRole("option", "other", {exact:true}).waitFor();
  await search.fill("feature/οσ");
  await screen.getByRole("option", /^feature\/\s*ΟΣΑ$/, {exact:true}).waitFor();
  await app.screenshot("comparison-lowercase-control");
  await search.fill("feature/ΟΣ");
  await app.screenshot("comparison-uppercase-prefix");
  const comparisonFound = await screen.getByRole("option", /^feature\/\s*ΟΣΑ$/, {exact:true}).isVisible();
  if (comparisonFound) {
    await screen.getByRole("option", /^feature\/\s*ΟΣΑ$/, {exact:true}).tap();
    await app.screenshot("comparison-original-ref-selected");
  }
  expect({toolbarFound, selected, comparisonFound}).toEqual({toolbarFound:true, selected:"feature/ΟΣΑ", comparisonFound:true});
});
