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

test("Greek sigma prefixes find and preserve the original branch", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("greek-repo");
  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);
  const branch = browser.locator('[data-slot="combobox-trigger"][aria-label="Branch: main"]:visible').first();
  await branch.tap();
  const search = screen.getByPlaceholder("Search refs...");
  const result = screen.getByRole("option", /^feature\/\s*ΟΣΑ$/);
  await result.waitFor();
  await search.fill("feature/οσ");
  await result.waitFor();
  await app.screenshot("branch-lowercase-control");
  await search.fill("feature/ΟΣ");
  await app.screenshot("branch-uppercase-sigma");
  const branchFound = await result.count();
  await browser.keyboard.press("Escape");
  await screen.getByRole("button", "Toggle right panel", { exact: true }).tap();
  await screen.getByText("Diff", { exact: true }).tap();
  const comparison = screen.getByRole("combobox", /^Change comparison target/);
  await comparison.tap();
  await result.waitFor();
  await search.fill("feature/οσ");
  await result.waitFor();
  await app.screenshot("comparison-lowercase-control");
  await search.fill("feature/ΟΣ");
  await app.screenshot("comparison-uppercase-sigma");
  const comparisonFound = await result.count();
  if (comparisonFound) {
    await result.tap();
    await screen.getByRole("combobox", "Change comparison target. Currently feature/ΟΣΑ", { exact: true }).waitFor();
    await app.screenshot("comparison-original-ref-selected");
  }
  expect({ branchFound, comparisonFound }).toEqual({ branchFound: 1, comparisonFound: 1 });
});
