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

test("Idle numbered terminal names follow Japanese after navigation and process exit", { ...WEB, timeout: 180_000 }, async ({ t3, app, screen, browser }) => {
  await browser.emulateMedia({ colorScheme: "dark" });
  const project = await t3.project("terminal-demo");
  const first = await t3.thread({ project, title: "Terminal language", messages: ["hello"] });
  const second = await t3.thread({ project, title: "Other thread", messages: ["hello"] });
  await t3.open("/settings/general");
  await screen.getByRole("combobox", "Language").tap();
  await screen.getByRole("option", "日本語").tap();
  await screen.getByLabel("言語", { exact: true }).waitFor();
  await t3.open(first);
  await screen.getByRole("button", "ターミナルドロワーの切り替え").tap();
  const input = browser.locator('[aria-label="ターミナルの入力"][data-workspace-focus-default]');
  await input.waitFor();
  await input.focus();
  await browser.keyboard.type("PS1='$ '; clear");
  await browser.keyboard.press("Enter");
  await screen.getByRole("button", /^新しいターミナル/).tap();
  await input.focus();
  await browser.keyboard.type("PS1='$ '; clear");
  await browser.keyboard.press("Enter");
  await t3.open(second);
  await t3.open(first);
  const idle = screen.getByRole("button", /^(Terminal|ターミナル) 2$/);
  await idle.waitFor({ state: "visible", timeout: 90_000 });
  await idle.tap();
  await app.screenshot("idle-after-thread-return");
  const returned = await idle.textContent();
  await input.focus();
  await browser.keyboard.type("sleep 60");
  await browser.keyboard.press("Enter");
  await screen.getByRole("button", "sleep", { exact: true }).waitFor({ state: "visible", timeout: 30_000 });
  await app.screenshot("running-sleep");
  await input.focus();
  await browser.keyboard.press("Control+c");
  await idle.waitFor({ state: "visible", timeout: 90_000 });
  await app.screenshot("idle-after-control-c");
  const stopped = await idle.textContent();
  expect(returned).toBe("ターミナル 2");
  expect(stopped).toBe("ターミナル 2");
});
