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

test("A prompt stashed in another document appears in the open stash", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const reading = await t3.thread({ project, title: "Reading", messages: ["Read the project"] });
  const writing = await t3.thread({ project, title: "Writing", messages: ["Write a draft"] });
  await t3.open(reading);
  await screen.getByRole("textbox").tap();
  await browser.keyboard.press("Control+s");
  await screen.getByText("Nothing stashed yet.", { exact: false }).waitFor();
  await browser.evaluate((threadId) => {
    const frame = document.createElement("iframe");
    frame.id = "second-client";
    frame.src = location.pathname.replace(/[^/]+$/, threadId);
    frame.style.cssText = "position:fixed;inset:0;width:100%;height:100%;border:0;z-index:2147483647";
    document.body.append(frame);
    return null;
  }, writing.threadId);
  const other = browser.frameLocator("#second-client");
  await other.getByRole("textbox").fill("Draft from the other tab");
  await browser.keyboard.press("Control+s");
  await other.getByRole("button", "Stashed prompts: 1. Open stash.").waitFor();
  await app.screenshot("other-tab-stashed-prompt");
  await browser.evaluate(() => {
    document.querySelector("#second-client")!.remove();
    return null;
  });
  await app.screenshot("stash-after-other-tab-saves");
  await expect(screen.getByRole("button", "Restore stashed prompt: Draft from the other tab")).toBeVisible();
});
