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

test("An agent cannot archive its active thread and can finish its turn", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("archive-demo");
  await t3.open();
  await screen.getByRole("textbox").fill('Call t3_thread_organize with {"action":"archive"}, omit threadId, then reply done.');
  await screen.getByRole("textbox").press("Enter");
  let threadPath = "";
  try {
    await expect(screen.getByText("Stop it before archiving", { exact: false })).toBeVisible();
    await expect(screen.getByText("done", { exact: true })).toBeVisible();
    await expect(screen.getByRole("button", "Stop generation")).toBeHidden();
    await expect(screen.getByText("Syncing messages...", { exact: false })).toBeHidden();
  } finally {
    await app.screenshot("archive-response");
    threadPath = await browser.evaluate(() => location.pathname);
    await app.open("/settings/archived");
    await browser.locator("#archive").waitFor();
    await app.screenshot("archive-list-during-request");
    await app.open(threadPath);
  }
  await expect(screen.getByText("done", { exact: true })).toBeVisible();
  await expect(screen.getByRole("button", "Stop generation")).toBeHidden();
  await expect(screen.getByTestId("sidebar-row-card")).toBeVisible();
  await expect(screen.getByText("Syncing messages...", { exact: false })).toBeHidden();
  await app.screenshot("turn-completed");
  await screen.getByRole("button", "Thread actions for", { exact: false }).tap();
  await screen.getByRole("menuitem", "Archive thread", { exact: true }).tap();
  await expect(screen.getByTestId("sidebar-row-card")).toBeHidden();
  await app.open("/settings/archived");
  await expect(screen.getByRole("button", "Unarchive", { exact: false })).toBeVisible();
  await app.screenshot("archive-after-turn");
});
