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

test("Stashing a failed file upload reports retry or remove and retains the draft", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("notes-demo");
  const thread = await t3.thread({ project, title: "Notes", messages: ["Ready"] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.fill("Summarize these notes");
  await browser.route("**/api/attachments/upload/**", route => route.abort());
  await browser.evaluate(() => {
    const input = document.querySelector<HTMLInputElement>('input[type="file"]');
    if (!input) throw new Error("Composer attachment input missing");
    const files = new DataTransfer();
    files.items.add(new File(["Synthetic meeting notes.\n".repeat(40)], "notes.txt", { type: "text/plain" }));
    input.files = files.files;
    input.dispatchEvent(new Event("change", { bubbles: true }));
  });
  await screen.getByText("upload failed", { exact: false }).waitFor();
  await composer.tap();
  await browser.keyboard.press("Control+s");
  await browser.locator('[data-slot="toast-title"]').waitFor();
  await browser.evaluate(async () => {
    await Promise.all(document.getAnimations().filter(animation => animation.effect?.getTiming().iterations !== Infinity).map(animation => animation.finished.catch(() => {})));
  });
  await app.screenshot("failed-upload-stash");
  await expect(screen.getByText("Retry or remove failed file uploads before stashing this prompt", { exact: true })).toBeVisible();
  await expect(browser.locator('[data-prompt-stash-badge="true"]')).toBeHidden();
  await expect(composer).toContainText("Summarize these notes");
  await expect(screen.getByText("upload failed", { exact: false })).toBeVisible();
});
