/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Restoring a duplicate file leaves only its backed chip", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("demo");
  await t3.open();
  const composer = screen.getByRole("textbox");
  const attach = async () => {
    await browser.evaluate(() => {
      const input = document.querySelector('form[data-chat-composer-form] input[type="file"]') as HTMLInputElement;
      if (!input) throw new Error("No attachment input");
      const transfer = new DataTransfer();
      transfer.items.add(new File(["%PDF-1.4\n%%EOF"], "report.pdf", { type: "application/pdf", lastModified: 1700000000000 }));
      input.files = transfer.files;
      input.dispatchEvent(new Event("change", { bubbles: true }));
    });
    await browser.locator('[contenteditable="true"]').getByText("1 KB", { exact: true }).waitFor();
  };
  await composer.fill("Summarise ");
  await attach();
  await composer.press("ControlOrMeta+s");
  await screen.getByRole("button", /Stashed prompts: 1/).waitFor();
  await attach();
  await screen.getByRole("button", /Stashed prompts: 1/).tap();
  await screen.getByRole("button", /Restore stashed prompt: Summarise/).tap();
  await app.screenshot("restored-duplicate-file");
  await expect(browser.locator('[aria-label="Unavailable context, report.pdf"]')).toHaveCount(0);
  await expect(browser.locator('[contenteditable="true"]').getByText("1 KB", { exact: true })).toHaveCount(1);
});
