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

test("An ordinary ACP thread already owns its native session", WEB, async ({t3, app, screen}) => {
  const project = await t3.project("proj");
  await t3.open("/settings/providers");
  await screen.getByRole("button", "Add provider", {exact:true}).first().tap();
  await screen.getByRole("button", "Show more").tap();
  await screen.getByRole("button", "Local ACP command").tap();
  await screen.getByRole("textbox", "Executable").fill("opencode");
  await screen.getByRole("button", "Add argument").tap();
  await screen.getByRole("textbox", "Argument 1").fill("acp");
  await screen.getByRole("button", "Next", {exact:true}).tap();
  await screen.getByRole("button", "Add instance", {exact:true}).tap();
  const cli = makeState(T3_HOME).cli;
  const launched = await runPromise(cli(["thread", "start", project, "Reply with just the word hello. Do not use any tools.", "--title", "Hello Reply", "--provider", "acpRegistry_local_acp", "--model", "default", "--wait", "--json"]));
  const thread = JSON.parse(launched.stdout);
  await t3.open(thread);
  await t3.open("/settings/providers");
  await screen.getByRole("button", "Local ACP", {exact:false}).tap();
  await screen.getByRole("button", "Refresh provider status", {exact:false}).tap();
  await screen.getByRole("combobox", "Project for ACP sessions").waitFor({state:"visible", timeout:90000});
  await screen.getByRole("combobox", "Project for ACP sessions").tap();
  await screen.getByRole("option", "proj", {exact:true}).tap();
  await screen.getByRole("button", "List sessions", {exact:true}).tap();
  await screen.getByRole("button", "Refresh", {exact:true}).waitFor({state:"visible"});
  await app.screenshot("owned-session");
  await expect(screen.getByRole("button", "Imported", {exact:true})).toBeVisible();
});
