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

const prompt = "Create a goal. Reply PHASE_ONE_DONE now, then reply PHASE_TWO_DONE in the automatic goal continuation.";

test("ACP v2 echoes once and retains autonomous work after idle", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("ACP demo");
  const state = makeState(T3_HOME);
  const started = await runPromise(state.cli(["thread", "start", project, "Reply with exactly: ACP_FIRST_OK", "--title", "ACP v2 proof", "--provider", "acpProof", "--model", "default", "--wait", "--json"]));
  const thread = JSON.parse(started.stdout);
  await t3.open({threadId:thread.threadId});
  await screen.getByRole("textbox").fill(prompt);
  await screen.getByRole("textbox").press("Enter");
  // Allow the fixture's idle -> running -> output -> idle sequence to finish on both revisions.
  await new Promise(resolve => setTimeout(resolve, 9000));
  await app.screenshot("autonomous-work");
  await expect(screen.getByText(prompt, {exact:true})).toHaveCount(1);
  await expect(screen.getByText("PHASE_TWO_DONE", {exact:true})).toBeVisible();
  await expect(screen.getByRole("button", "Stop generation")).toBeHidden();
  await browser.reload();
  await expect(screen.getByText("PHASE_TWO_DONE", {exact:true})).toBeVisible();
  await app.screenshot("after-reload");
});
