/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Background PR panel lets Settings record Ctrl+Shift+K", WEB, async ({ t3, app, browser, screen }) => {
  await browser.setViewport({ width: 1440, height: 1000 });
  await browser.addInitScript(() => {
    (window as any).copyLog = [];
    Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText: async (text: string) => { (window as any).copyLog.push(text); } } });
  });
  const project = await t3.project("shortcut-proof", { ".git/config": '[remote "origin"]\n url = https://github.com/pingdotgg/t3code.git\n fetch = +refs/heads/*:refs/remotes/origin/*\n' });
  const thread = await t3.thread({ project, title: "PR shortcut proof", messages: ["Review https://github.com/pingdotgg/t3code/pull/45236"] });
  await t3.open(thread);
  await screen.getByRole("button", "Agent Review https://github.com/pingdotgg/t3code/pull/45236", { exact: true }).tap();
  await screen.getByRole("link", "https://github.com/pingdotgg/t3code/pull/45236", { exact: true }).tap();
  await screen.getByRole("button", "Open pull request #45236 on host", { exact: true }).waitFor();
  await screen.getByRole("button", "Settings", { exact: true }).tap();
  await screen.getByRole("button", "Keybindings", { exact: true }).tap();
  await screen.getByLabel("Search keybindings").fill("Command Palette");
  await screen.getByRole("button", "Edit shortcut for Command Palette: Toggle: Ctrl+K", { exact: true }).tap();
  const capture = screen.getByRole("textbox", "Keybinding for Command Palette: Toggle", { exact: true });
  await capture.press("Control+Shift+k");
  const recorded = await browser.locator('[data-keybinding-capture]').inputValue();
  const backgroundCopies = await browser.evaluate(() => (window as any).copyLog.slice());
  await browser.evaluate(() => {
    const log = document.createElement("pre");
    log.id = "proof-observation";
    log.style.cssText = "position:fixed;bottom:16px;left:280px;z-index:2147483647;background:#101827;color:white;padding:12px;border:2px solid #94a3b8;font:16px monospace;pointer-events:none";
    log.textContent = "Observed after Ctrl+Shift+K\nRecorder: " + JSON.stringify((document.querySelector("[data-keybinding-capture]") as HTMLInputElement).value) + "\nClipboard writes: " + JSON.stringify((window as any).copyLog);
    document.body.append(log);
  });
  await app.screenshot("settings-records-copy-number-chord");
  await browser.evaluate(() => document.getElementById("proof-observation")?.remove());
  await capture.press("Control+Shift+j");
  await capture.press("Escape");
  await screen.getByRole("button", "Close", { exact: true }).tap();
  await screen.getByRole("dialog").waitFor({ state: "hidden" });
  await browser.evaluate(() => { (window as any).copyLog = []; });
  await screen.getByRole("button", "Open pull request #45236 on host", { exact: true }).press("Control+Shift+k");
  const foregroundCopies = await browser.evaluate(() => (window as any).copyLog.slice());
  await browser.evaluate(() => {
    const log = document.createElement("pre");
    log.style.cssText = "position:fixed;bottom:16px;left:280px;z-index:2147483647;background:#101827;color:white;padding:12px;border:2px solid #94a3b8;font:16px monospace;pointer-events:none";
    log.textContent = "Observed in foreground after Ctrl+Shift+K\nClipboard writes: " + JSON.stringify((window as any).copyLog);
    document.body.append(log);
  });
  await app.screenshot("foreground-panel-copy-number");
  expect({ recorded, backgroundCopies, foregroundCopies }).toEqual({ recorded: "mod+shift+k", backgroundCopies: [], foregroundCopies: ["#45236"] });
});
