import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Cursor Agent input is readable in the light terminal", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Cursor input readability", messages: ["Ready"] });
  await t3.open(thread);
  await browser.evaluate(() => {
    localStorage.setItem("t3code:theme", "light");
    localStorage.setItem("t3code:theme-follow-system", "false");
    localStorage.setItem("t3code:theme-appearance-mode", "light");
  });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle terminal drawer").tap();
  await browser.locator('canvas:has(~ textarea[aria-label="Terminal input"])').tap();
  await browser.keyboard.type('clear; HOME="$(dirname "$PWD")" agent --trust');
  await browser.keyboard.press("Enter");
  await new Promise(resolve => setTimeout(resolve, 10000));
  await browser.keyboard.type("You cannot see what I am typing in here");
  await new Promise(resolve => setTimeout(resolve, 2000));
  await app.screenshot("cursor-light-input");
  const darkBand = await browser.evaluate(() => {
    const canvas = document.querySelector('canvas:has(~ textarea[aria-label="Terminal input"])') as HTMLCanvasElement;
    const context = canvas.getContext("2d")!;
    const { width, height } = canvas;
    const pixels = context.getImageData(0, 0, width, height).data;
    for (let y = 0; y < height; y++) {
      let dark = 0;
      for (let x = 0; x < width; x++) {
        const i = (y * width + x) * 4;
        if (pixels[i] < 40 && pixels[i + 1] < 40 && pixels[i + 2] < 40) dark++;
      }
      if (dark > width * 0.8) return true;
    }
    return false;
  });
  expect(darkBand).toBe(false);
});
