import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB, makeState, runPromise } from "./t3.ts";

test("Recovered queued edit retains its thread mention", WEB, async ({ t3, app, screen }) => {
  const project = await t3.project("Recovery demo");
  await t3.thread({ project, title: "Reference notes", messages: ["Reference material"] });
  const active = await t3.thread({ project, title: "Queued edit recovery", messages: ["Wait for approval"], model: "approval" });
  await t3.open(active);
  const composer = screen.getByRole("textbox");
  await screen.getByRole("button", "Write approval", { exact: true }).tap();
  await screen.getByRole("button", "Permissions: Full access", { exact: true }).tap();
  await screen.getByRole("menuitemradio", "Supervised", { exact: true }).tap();
  await composer.press("Escape");
  await composer.fill("Wait for approval again");
  await composer.press("Enter");
  await screen.getByRole("button", "Approve", { exact: true }).waitFor({ state: "visible" });
  await runPromise(makeState(T3_HOME).cli(["thread", "send", active.threadId, "Follow up on the findings"]));
  await screen.getByRole("button", "Edit queued message", { exact: true }).tap();
  await composer.fill("Unsaved revision ");
  await composer.pressSequentially("@Reference");
  await screen.getByText("Reference notes", { exact: true }).last().tap();
  await app.screenshot("unsaved-edit");
  await screen.getByRole("button", "Approve", { exact: true }).tap();
  await screen.getByText("was kept in the composer.", { exact: false }).waitFor({ state: "visible" });
  await app.screenshot("recovered-edit");
  await expect(composer).toContainText("Reference notes");
});
