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

test("Queued edits preserve attachment chips, preview actions, and undo/redo", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Attachment demo", { "src/math.ts": "export const sum = 2;" });
  const thread = await t3.thread({ project, title: "Queued attachments", messages: ["Initial tour"], model: "showcase" });
  await t3.open("/settings/chat");
  await screen.getByRole("combobox", "Tool calls in chat").tap();
  await screen.getByRole("option", "Hide all").tap();
  await t3.open(thread);
  const editor = screen.getByRole("textbox");
  await editor.fill("Start another tour");
  await editor.press("Enter");
  await screen.getByRole("button", "Stop generation").hover();
  await editor.fill("Please inspect these attachments: ");
  await browser.evaluate(async () => {
    const canvas = document.createElement("canvas"); canvas.width = 480; canvas.height = 240;
    const ctx = canvas.getContext("2d")!;
    ctx.fillStyle = "#284d82"; ctx.fillRect(0, 0, 480, 240);
    ctx.fillStyle = "#ffffff"; ctx.font = "bold 32px sans-serif"; ctx.fillText("QUEUED IMAGE", 90, 125);
    const blob = await new Promise<Blob>(resolve => canvas.toBlob(blob => resolve(blob!), "image/png"));
    await navigator.clipboard.write([new ClipboardItem({ "image/png": blob })]);
  });
  await editor.press("Control+v");
  await screen.getByRole("button", "Remove image.png").hover();
  await browser.evaluate(() => {
    const input = document.querySelector('input[type="file"]') as HTMLInputElement;
    const data = new DataTransfer();
    data.items.add(new File(["Synthetic attachment for queued-message proof.\n"], "notes.txt", { type: "text/plain" }));
    input.files = data.files; input.dispatchEvent(new Event("change", { bubbles: true }));
  });
  await browser.locator('[contenteditable="true"] [aria-label^="File attachment,"]').hover();
  await editor.press("Control+Enter");
  await screen.getByRole("button", "Edit queued message").hover();
  await screen.getByRole("button", "Stop generation").tap();
  await screen.getByRole("button", "Edit queued message").tap();
  const clearSelection = async () => { await editor.focus(); await editor.press("Control+End"); await editor.press("ArrowRight"); };
  const state = () => browser.evaluate(() => ({
    unavailable: document.querySelectorAll('[contenteditable="true"] [aria-label^="Unavailable context"]').length,
    imageChip: document.querySelectorAll('[contenteditable="true"] [aria-label^="Image attachment,"]').length,
    fileChip: document.querySelectorAll('[contenteditable="true"] [aria-label^="File attachment,"]').length,
    imageShelf: document.querySelectorAll('button[aria-label="Remove image.png"]').length,
    fileShelf: document.querySelectorAll('button[aria-label="Remove notes.txt"]').length,
  }));
  await clearSelection();
  await app.screenshot("01-edit-queued");
  const edited = await state();
  if (edited.imageChip) {
    await browser.locator('[contenteditable="true"] [aria-label^="Image attachment,"]').tap();
    await app.screenshot("02-image-preview");
    await browser.keyboard.press("Escape");
  }
  if (edited.fileChip) {
    await browser.locator('[contenteditable="true"] [aria-label^="File attachment,"]').tap();
    await screen.getByText("Synthetic attachment for queued-message proof.", { exact: false }).hover();
    await app.screenshot("03-file-open");
    await screen.getByRole("button", "Toggle right panel").tap();
  }
  await screen.getByRole("button", "Remove image.png").tap();
  await clearSelection(); await app.screenshot("04-image-removed");
  const removed = await state();
  await editor.press("Control+z");
  await clearSelection(); await app.screenshot("05-image-undo");
  const undone = await state();
  await editor.press("Control+Shift+z");
  await clearSelection(); await app.screenshot("06-image-redo");
  const redone = await state();
  await screen.getByRole("button", "Remove notes.txt").tap();
  await clearSelection(); await app.screenshot("07-file-removed");
  const fileRemoved = await state();
  await editor.press("Control+z");
  await clearSelection(); await app.screenshot("08-file-undo");
  const fileUndone = await state();
  await editor.press("Control+Shift+z");
  await clearSelection(); await app.screenshot("09-file-redo");
  const fileRedone = await state();
  await expect(edited).toEqual({ unavailable: 0, imageChip: 1, fileChip: 1, imageShelf: 1, fileShelf: 1 });
  await expect(removed.imageChip).toBe(0); await expect(removed.imageShelf).toBe(0);
  await expect(undone.imageChip).toBe(1); await expect(undone.imageShelf).toBe(1);
  await expect(redone.imageChip).toBe(0); await expect(redone.imageShelf).toBe(0);
  await expect(fileRemoved.fileChip).toBe(0); await expect(fileRemoved.fileShelf).toBe(0);
  await expect(fileUndone.fileChip).toBe(1); await expect(fileUndone.fileShelf).toBe(1);
  await expect(fileRedone.fileChip).toBe(0); await expect(fileRedone.fileShelf).toBe(0);
});
