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

test("Japanese terminal excerpt ranges match their preview in drafts and sent messages", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Terminal excerpts", messages: ["Start"] });
  await t3.open("/settings/general");
  await screen.getByLabel("Language", { exact: true }).tap();
  await screen.getByText("日本語", { exact: true }).tap();
  await screen.getByLabel("言語", { exact: true }).waitFor();
  await t3.open(thread);
  await screen.getByRole("button", "ターミナルドロワーの切り替え").tap();
  await screen.getByRole("button", /^新しいターミナル(?: \(|$)/).tap();
  const input = browser.locator('[aria-label="ターミナルの入力"][data-workspace-focus-default]');
  await input.waitFor();
  const actual: string[] = [];
  for (const [index, output] of [[1, "hello from terminal two"], [2, "first row\\r\\nsecond row"]] as const) {
    if (index === 2) {
      await input.focus();
      await browser.keyboard.press("Control+C");
    }
    await input.focus();
    await browser.keyboard.type(`printf '\\033[3J\\033[2J\\033[H${output}'; sleep 600`);
    await browser.keyboard.press("Enter");
    await screen.getByRole("button", "sleep", { exact: true }).waitFor({ timeout: 40000 });
    await input.focus();
    await browser.keyboard.press("Control+Shift+A");
    await browser.evaluate(() => {
      const canvas = document.activeElement?.parentElement?.querySelector("canvas");
      if (!canvas) throw new Error("No terminal canvas");
      const { x, y } = canvas.getBoundingClientRect();
      canvas.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true, cancelable: true, button: 2, clientX: x + 20, clientY: y + 20 }));
    });
    await screen.getByText("チャットに追加", { exact: true }).tap();
    const chip = screen.getByRole("button", /ターミナルの抜粋、/).last();
    await chip.waitFor();
    actual.push(await chip.textContent() ?? "");
    await chip.tap();
    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(`draft-${index}-preview`);
    await browser.keyboard.press("Escape");
    await screen.getByRole("button", "メッセージを送信", { exact: true }).tap();
    const sent = screen.getByRole("button", /ターミナルの抜粋、/).last();
    await sent.waitFor();
    actual.push(await sent.textContent() ?? "");
    await sent.tap();
    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(`sent-${index}-preview`);
    if (index === 1) await browser.keyboard.press("Escape");
  }
  expect(actual).toEqual(["sleep 1 行目", "sleep 1 行目", "sleep 1–2 行目", "sleep 1–2 行目"]);
});
