import { existsSync, rmSync } from "node:fs";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Integrated terminal starts the complete configured shell path", WEB, async ({ t3, app, screen, browser }) => {
  const marker = process.env.T3_PROOF_MARKER!;
  rmSync(marker, { force: true });
  const project = await t3.project("shell-path-proof");
  const thread = await t3.thread({ project, title: "Shell path with spaces", messages: ["go"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle terminal drawer", { exact: true }).tap();
  try {
    await expect.poll(async () => {
      if (!existsSync(marker)) return false;
      return browser.evaluate(() => {
        const canvas = document.querySelector<HTMLCanvasElement>("[data-thread-terminal-drawer] canvas");
        if (!canvas || document.body.textContent?.includes("Syncing messages")) return false;
        const context = canvas.getContext("2d");
        if (!context || canvas.width === 0 || canvas.height === 0) return false;
        const pixels = context.getImageData(0, 0, canvas.width, Math.min(120, canvas.height)).data;
        const colors = new Set<number>();
        for (let i = 0; i < pixels.length; i += 4) colors.add((pixels[i]! << 16) | (pixels[i + 1]! << 8) | pixels[i + 2]!);
        return colors.size > 16;
      });
    }, { timeout: 10000 }).toBe(true);
  } finally {
    await app.screenshot("configured-shell");
  }
});
