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

test("Encoded POSIX backslash images keep their literal filename", WEB, async ({ t3, app, browser, screen }) => {
  const blue = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAPAAAABkCAIAAAA3wCqQAAABVUlEQVR4nO3OAQnAMAAEsUqcfzOdiYOHEoiAnPNdeMd+AKH9AEL7AYT2AwjtBxDaDyC0H0BoP4DQfgCh/QBC+wGE9gMI7QcQ2g8gtB9AaD+A0H4Aof0AQvsBhPYDCO0HENoPILQfQGg/gNB+AKH9AEL7AYT2AwjtBxDaDyC0H0BoP4DQfgCh/QBC+wGE9gMI7QcQ2g8gtB9AaD+A0H4Aof0AQvsBhPYDCO0HENoPILQfQGg/gNB+AKH9AEL7AYT2AwjtBxDaDyC0H0BoP4DQfgCh/QBC+wGE9gMI7QcQ2g8gtB9AaD+A0H4Aof0AQvsBhPYDCO0HENoPILQfQGg/gNB+AKH9AEL7AYT2AwjtBxDaDyC0H0BoP4DQfgCh/QBC+wGE9gMI7QcQ2g8gtB9AaD+A0H4Aof0AQvsBhPYDCO0HENoPILQfQGg/gNB+AKH9AEL7AXR+IL61ZwomDLoAAAAASUVORK5CYII=", "base64");
  const red = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAPAAAABkCAIAAAA3wCqQAAABJklEQVR4nO3SQQkAMAzAwIqof6ubicEgHJyAPDJnFzLmewE8ZGhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE2KoUkxNCmGJsXQpBiaFEOTYmhSDE3KBYuwDpVv6p4/AAAAAElFTkSuQmCC", "base64");
  const project = await t3.project("image-demo", { "a\\b.png": blue });
  const markdown = `Relative image (must be blue):

![Relative screenshot](./a%5Cb.png)

Absolute control (blue):

![Absolute screenshot](${project}/a%5Cb.png)`;
  const thread = await t3.thread({ project, title: "Encoded filename images", model: "default", messages: [markdown] });
  await t3.open(thread);
  await screen.getByRole("button", /^Agent Relative image/).tap();
  await browser.evaluate(() => { document.querySelector("[data-user-message-body]")!.scrollIntoView({block: "start"}); return null; });
  const control = browser.locator('img[alt="Absolute screenshot"]');
  await control.waitFor();
  await browser.evaluate(async () => { await Promise.all(Array.from(document.querySelectorAll('img')).map(img => img.decode().catch(() => {}))); return null; });
  await browser.locator("[data-user-message-body]").scrollIntoView();
  await app.screenshot("missing-nested-file");
  const first = await browser.evaluate(() => {
    const img = document.querySelector<HTMLImageElement>('img[alt="Relative screenshot"]');
    return img?.naturalWidth ?? 0;
  });
  await mkdir(`${project}/a`, { recursive: true });
  await writeFile(`${project}/a/b.png`, red);
  await t3.open(thread);
  await screen.getByRole("button", /^Agent Relative image/).tap();
  await browser.evaluate(() => { document.querySelector("[data-user-message-body]")!.scrollIntoView({block: "start"}); return null; });
  await browser.locator('img[alt="Relative screenshot"]').waitFor();
  await browser.evaluate(async () => { await Promise.all(Array.from(document.querySelectorAll('img')).map(img => img.decode().catch(() => {}))); return null; });
  await browser.locator("[data-user-message-body]").scrollIntoView();
  await app.screenshot("red-decoy-present");
  const pixel = await browser.evaluate(async () => {
    const image = document.querySelector<HTMLImageElement>('img[alt="Relative screenshot"]')!;
    const canvas = document.createElement("canvas"); canvas.width = canvas.height = 1;
    const ctx = canvas.getContext("2d")!; ctx.drawImage(image, 0, 0, 1, 1);
    return Array.from(ctx.getImageData(0, 0, 1, 1).data);
  });
  expect({ widthWithoutDecoy: first, relativePixelWithDecoy: pixel }).toEqual({ widthWithoutDecoy: 240, relativePixelWithDecoy: [0, 80, 255, 255] });
});
