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

test("Mounted terminal accessible names follow the language", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Terminal language", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle terminal drawer").tap();
  const input = browser.locator('[aria-label="Terminal input"]');
  await input.waitFor();
  await input.focus();
  await browser.keyboard.type("PS1='$ '; clear; seq 1 300\n");
  await browser.evaluate(() => {
    document.querySelector('[aria-label="Terminal input"]')!.setAttribute("data-proof-original", "true");
    return null;
  });
  await screen.getByRole("button", "Settings", { exact: true }).tap();
  await screen.getByLabel("Language", { exact: true }).tap();
  await screen.getByText("日本語", { exact: true }).tap();
  await screen.getByLabel("言語", { exact: true }).waitFor();
  await browser.keyboard.press("Escape");
  await screen.getByRole("button", "ターミナルドロワーの切り替え").waitFor();
  await browser.evaluate(async () => {
    await Promise.all(document.getAnimations().map(a => a.finished.catch(() => {})));
    return null;
  });
  const observed = await browser.evaluate(() => {
    const input = document.querySelector('[data-proof-original="true"]');
    const scrollbar = document.querySelector('[role="scrollbar"]');
    const values = { input: input?.getAttribute("aria-label"), scrollbar: scrollbar?.getAttribute("aria-label"), sameNode: !!input, language: document.documentElement.lang };
    const panel = document.createElement("pre");
    panel.textContent = "Live terminal accessibility inspection\n" + JSON.stringify(values, null, 2);
    panel.style.cssText = "position:fixed;top:70px;right:24px;z-index:99999;padding:20px;background:#111;color:#fff;border:2px solid #aaa;font:18px monospace;";
    document.body.appendChild(panel);
    return values;
  });
  await app.screenshot("terminal-after-language-switch");
  expect(observed).toEqual({ input: "ターミナルの入力", scrollbar: "ターミナルのスクロールバック", sameNode: true, language: "ja" });
});
