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

test("Project action shortcut recorder owns bound chords", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("shortcut-demo");
  const thread = await t3.thread({ project, title: "Shortcut capture", messages: ["Start"] });
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open(thread);
  let saved = false;
  const openEditor = async () => {
    if (!saved) await screen.getByRole("button", "Add project script", { exact: true }).tap();
    else {
      await screen.getByRole("button", "Script actions", { exact: true }).tap();
      await screen.getByRole("menuitem", "Add project script", { exact: true }).tap();
    }
  };
  const value = () => browser.evaluate(() => document.querySelector<HTMLInputElement>('#script-keybinding')?.value ?? '<closed>');
  const observed: Record<string, string> = {};
  const capture = async (label: string) => {
    await browser.evaluate(async () => {
      await Promise.all(document.getAnimations().filter(animation => animation.effect?.getComputedTiming().iterations !== Infinity).map(animation => animation.finished.catch(() => {})));
      return null;
    });
    await app.screenshot(label);
  };
  await openEditor();
  const key = browser.locator('#script-keybinding');
  await key.tap();
  await browser.keyboard.press('Control+b');
  observed.sidebarChord = await value();
  await capture('ctrl-b-recorder-and-sidebar');
  await browser.keyboard.press('Backspace');
  await browser.keyboard.press('Control+Shift+k');
  observed.unboundControl = await value();
  await browser.keyboard.press('Backspace');
  await browser.keyboard.press('Control+s');
  observed.stashChord = await value();
  await capture('ctrl-s-recorder');
  await browser.keyboard.press('Backspace');
  await browser.keyboard.press('Control+Shift+y');
  observed.unbound = await value();
  await browser.keyboard.press('Control+Escape');
  await capture('modified-escape');
  observed.modifiedEscape = await value();
  if (observed.modifiedEscape !== '<closed>') await browser.keyboard.press('Escape');
  await openEditor();
  await browser.locator('#script-name').fill('Build');
  await browser.locator('#script-command').fill('echo BUILD-RAN');
  await key.tap();
  await browser.keyboard.press('Control+Shift+y');
  await screen.getByRole('button', 'Save action', { exact: true }).tap();
  saved = true;
  await openEditor();
  await key.tap();
  await browser.keyboard.press('Control+Shift+y');
  observed.existingAction = await value();
  await capture('existing-action-chord');
  await key.tap();
  await browser.keyboard.press('Escape');
  await key.waitFor({ state: 'hidden' });
  observed.bareEscape = '<closed>';
  await capture('after-bare-escape-action-side-effects');
  expect(observed).toEqual({ sidebarChord: 'mod+b', unboundControl: 'mod+shift+k', stashChord: 'mod+s', unbound: 'mod+shift+y', modifiedEscape: 'mod+esc', existingAction: 'mod+shift+y', bareEscape: '<closed>' });
});
