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

test("Escape cancels PR description editing before closing the workspace sheet", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("escape-demo");
  execFileSync("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project });
  const thread = await t3.thread({ project, title: "Review pull request", model: "multi-turn", messages: ["Start"] });
  await browser.setViewport({ width: 900, height: 800 });
  await t3.open(thread);
  await screen.getByTestId("composer-editor").fill("Look at https://github.com/pingdotgg/t3code/pull/43118");
  await screen.getByTestId("composer-editor").press("Enter");
  await screen.getByRole("link", /43118/).first().click();
  await screen.getByRole("button", "Edit description", { exact: true }).click();
  await screen.getByRole("combobox", "Pull request description", { exact: true }).waitFor();
  await app.screenshot("editing-description");
  await browser.keyboard.press("Escape");
  await app.screenshot("first-escape");
  await expect(screen.getByRole("dialog", "Workspace panel", { exact: true })).toBeVisible();
  await expect(screen.getByRole("button", "Edit description", { exact: true })).toBeVisible();
  await browser.keyboard.press("Escape");
  await app.screenshot("second-escape");
  await expect(screen.getByRole("dialog", "Workspace panel", { exact: true })).not.toBeVisible();
});
