import { expect } from "e2e";
import { readFile } from "node:fs/promises";
import { setTimeout } from "node:timers/promises";
import { test, WEB } from "./t3.ts";

test("Script command preserves separate hyphens with JetBrains Mono", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("Script demo");
  await t3.open();
  await screen.getByRole("button", "Add project", { exact: true }).textContent();
  await app.open("/settings/appearance");
  await screen.getByRole("textbox", "Interface font family").fill("JetBrains Mono");
  await screen.getByRole("textbox", "Interface font family").press("Enter");
  await screen.getByRole("textbox", "Interface font family").press("Tab");
  
  await app.open("/");
  await screen.getByRole("button", "Add project script", { exact: true }).tap();
  await screen.getByRole("textbox", "Name", { exact: true }).fill("Start demo");
  await screen.getByRole("textbox", "Command", { exact: true }).fill("./scripts/start.sh --demo");
  await screen.getByRole("heading", "Add Action", { exact: true }).tap();
  await browser.evaluate(async () => { await document.fonts.ready; });
  let stable = false;
  for (let attempt = 0; attempt < 8; attempt++) {
    const first = await app.screenshot("stability-a");
    await setTimeout(500);
    const second = await app.screenshot("stability-b");
    if ((await readFile(first)).equals(await readFile(second))) { stable = true; break; }
  }
  if (!stable) throw new Error("Rendering did not stabilize");
  await app.screenshot("command-hyphens");
  const ligatures = await browser.evaluate(() => getComputedStyle(document.querySelector("#script-command")!).fontVariantLigatures);
  expect(ligatures, "Command must render adjacent hyphens separately").toBe("none");
});
