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

test("A lone wide user image keeps the entire settings row visible", WEB, async ({ t3, app, browser, screen }) => {
  await t3.project("Image example");
  await t3.open();
  const composer = screen.getByRole("textbox");
  await composer.fill("Why is this row misaligned?");
  await browser.evaluate(async () => {
    const canvas = new OffscreenCanvas(600, 40);
    const ctx = canvas.getContext("2d")!;
    ctx.fillStyle = "white";
    ctx.fillRect(0, 0, 600, 40);
    ctx.fillStyle = "#2563eb";
    ctx.fillRect(12, 8, 24, 24);
    ctx.fillStyle = "#111827";
    ctx.font = "18px sans-serif";
    ctx.fillText("Notifications: show desktop alerts", 50, 26);
    ctx.fillStyle = "#16a34a";
    ctx.beginPath();
    ctx.roundRect(540, 8, 48, 24, 12);
    ctx.fill();
    ctx.fillStyle = "white";
    ctx.beginPath();
    ctx.arc(576, 20, 9, 0, Math.PI * 2);
    ctx.fill();
    const png = await canvas.convertToBlob({ type: "image/png" });
    await navigator.clipboard.write([new ClipboardItem({ "image/png": png })]);
  });
  await browser.keyboard.press("ControlOrMeta+V");
  await expect(composer.getByRole("button", "Image attachment", { exact: false })).toHaveCount(1);
  await composer.press("Enter");
  const image = browser.locator('img[alt="image.png"]');
  await expect(image).toBeVisible();
  await app.screenshot("wide-settings-row");
  const wholeImageVisible = await browser.evaluate(() => {
    const image = document.querySelector('img[alt="image.png"]') as HTMLImageElement;
    const box = image.getBoundingClientRect();
    const fit = getComputedStyle(image).objectFit;
    const ratio = box.width / box.height;
    return fit === "contain" || Math.abs(ratio - image.naturalWidth / image.naturalHeight) < 0.1;
  });
  expect(wholeImageVisible).toBe(true);
});
