import { expect } from "e2e";

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

test("An explicitly renamed fork keeps its title after its first turn", WEB, async ({ t3, app, screen }) => {
  const project = await t3.project("Storage demo");
  const thread = await t3.thread({ project, title: "Earlier work", messages: ["Discuss storage options"] });
  await t3.open(thread);
  await screen.getByText("first fixture turn complete", { exact: false }).waitFor({ timeout: 180_000 });
  await screen.getByRole("button", "Fork from this response").first().tap();
  await screen.getByRole("button", /^Earlier work fork(?:,|$)/).secondaryTap();
  await screen.getByRole("menuitem", "Rename thread").tap();
  await screen.getByRole("textbox", "Thread title").fill("Storage notes");
  await screen.getByRole("textbox", "Thread title").press("Enter");
  await app.screenshot("renamed-before-first-message");
  await screen.getByRole("textbox").fill("Try SQLite instead");
  await screen.getByRole("textbox").press("Enter");
  await screen.getByText("first fixture turn complete", { exact: false }).nth(1).waitFor();
  await screen.getByRole("button", "Stop generation").waitFor({ state: "hidden" });
  await app.screenshot("after-first-turn");
  await expect(screen.getByRole("button", "Thread actions for Storage notes")).toBeVisible();
});
