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

test("A shell named team\\zsh stays usable", WEB, async ({ t3, app, browser, screen }) => {
  await browser.emulateMedia({ colorScheme: "dark" });
  const project = await t3.project("shell-demo");
  const thread = await t3.thread({ project, title: "Shell with a backslash", messages: ["hello"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle terminal drawer").tap();
  try {
    await expect.poll(async () => {
      const ready = await browser.evaluate(() => {
        const input = document.querySelector<HTMLElement>('[aria-label="Terminal input"][data-workspace-focus-default]');
        input?.focus();
        return !!input;
      });
      if (ready) {
        await browser.keyboard.type("printf 'SHELL_READY\\n' | tee shell-ready\n");
      }
      return readFile(`${project}/shell-ready`, "utf8").catch(() => "");
    }, { timeout: 15000 }).toBe("SHELL_READY\n");
  } finally {
    await app.screenshot("shell-after-opening");
  }
});
