import { writeFile } from "node:fs/promises";
import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("Trailing plus typos preserve the sidebar default and show a warning", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("Shortcut demo");
  await t3.open("/settings/keybindings");
  await screen.getByRole("searchbox", "Search keybindings").fill("Sidebar");
  await screen.getByRole("button", "Edit shortcut for Sidebar: Toggle: Ctrl+B").waitFor();
  await writeFile(`${T3_HOME}/userdata/keybindings.json`, JSON.stringify([
    { key: "mod+", command: "sidebar.toggle" },
    { key: "ctrl+shift+", command: "sidebar.toggle" },
    { key: "mod+++", command: "sidebar.toggle" }
  ], null, 2));
  // The screenshot follows the server's file-watch update on either revision.
  await screen.getByText(/Custom|Invalid keybindings configuration/, { exact: false }).first().waitFor();
  await browser.evaluate(async () => {
    await Promise.all(document.getAnimations().map((animation) => animation.finished.catch(() => {})));
  });
  await app.screenshot("trailing-plus-shortcuts");
  await expect(screen.getByText("Invalid keybindings configuration", { exact: true })).toBeVisible();
  await expect(screen.getByRole("button", "Edit shortcut for Sidebar: Toggle: Ctrl+B")).toBeVisible();
});
