import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

test("Semantic worktree naming explains an unavailable text generator", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("calculator", { "calculator.ts": "export const add = (a: number, b: number) => a + b;\n" });
  const { cli } = makeState(T3_HOME);
  await runPromise(cli(["thread", "start", project, "Add a subtract function to the calculator", "--title", "Calculator subtraction", "--provider", "devReplay", "--model", "multi-turn", "--worktree", "main", "--wait", "--json"], [0, 2]));
  await t3.open();
  await screen.getByRole("button", "Calculator subtraction, Done, calculator").tap();
  await screen.getByText("first fixture turn complete", { exact: true }).waitFor();
  await screen.getByText("Syncing messages...", { exact: false }).waitFor({ state: "hidden" });
  // Hide operational model labels and filesystem paths in public evidence only.
  await browser.evaluate(() => {
    const style = document.createElement("style");
    style.textContent = '[data-slot="sidebar-footer"] { visibility: hidden; }';
    document.head.appendChild(style);
    for (const button of document.querySelectorAll("button")) {
      if (["Multi turn", "Showcase"].includes(button.textContent?.trim() ?? "")) button.style.visibility = "hidden";
    }
  });
  try {
    await expect(screen.getByText("Could not rename the worktree branch, so it stays", { exact: false })).toBeVisible();
  } finally {
    const notice = screen.getByRole("button", /Could not rename the worktree branch, so it stays/);
    if (await notice.isVisible()) await notice.tap();
    await app.screenshot("worktree-naming-result");
  }
});
