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

test("A thread's own PR keeps thread context across repository casing", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("repository-case");
  const git = (args: string[]) => runPromise(run("git", args, { cwd: project }));
  await git(["remote", "add", "origin", "https://github.com/PingDotGG/T3Code.git"]);
  await git(["fetch", "--depth=1", "origin", "t3/claude-background-agent-errors"]);
  await git(["checkout", "-B", "t3/claude-background-agent-errors", "FETCH_HEAD"]);
  await git(["config", "branch.t3/claude-background-agent-errors.remote", "origin"]);
  await git(["config", "branch.t3/claude-background-agent-errors.merge", "refs/heads/t3/claude-background-agent-errors"]);
  const thread = await t3.thread({ project, title: "Review this branch's PR", messages: ["Please look at https://github.com/pingdotgg/t3code/pull/44395"] });
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open(thread);
  await screen.getByRole("button", /44395/).first().waitFor();
  await screen.getByRole("button", "Agent Please look at https://github.com/pingdotgg/t3code/pull/44395", { exact: true }).tap();
  await browser.locator('[aria-label="Conversation"] a[href="https://github.com/pingdotgg/t3code/pull/44395"]').first().click();
  await screen.getByText("Claude's background-work refusal says what changed", { exact: false }).first().waitFor();
  await screen.getByRole("button", "Squash and merge", { exact: true }).waitFor();
  const checkout = screen.getByRole("button", "Check out", { exact: true });
  const offersCheckout = await checkout.isVisible();
  if (offersCheckout) {
    await checkout.tap();
    await screen.getByText("In a separate worktree", { exact: false }).waitFor();
  }
  await app.screenshot("own-pr-panel");
  expect(offersCheckout, "The current thread's own PR must not offer another checkout").toBe(false);
});
