/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Pi rejects a positional prompt after --no-mcp and accepts the flag alone", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("Pi launch validation");
  await t3.open("/settings/providers");
  const add = async (label: string, args: string) => {
    await screen.getByRole("button", "Add provider", { exact: true }).first().tap();
    await screen.getByRole("button", "Show more", { exact: true }).tap();
    await screen.getByRole("radio", "Pi", { exact: true }).tap();
    await screen.getByRole("button", "Next", { exact: true }).tap();
    await screen.getByRole("textbox", "Label", { exact: true }).fill(label);
    await screen.getByRole("button", "Next", { exact: true }).tap();
    await screen.getByRole("textbox", "Binary path", { exact: true }).fill("pi");
    await screen.getByRole("textbox", "Launch arguments", { exact: true }).fill(args);
    await app.screenshot(label + "-arguments");
    await screen.getByRole("button", "Add instance", { exact: true }).tap();
  };
  await add("Pi flag alone", "--no-mcp");
  await screen.getByText("Authenticated", { exact: false }).first().waitFor({ state: "visible" });
  await app.screenshot("flag-alone-accepted");
  await add("Pi positional prompt", "--no-mcp 'fix the bug'");
  await screen.getByRole("button", "Refresh provider status", { exact: false }).first().tap();
  await screen.getByText("Checking Pi CLI availability...", { exact: false }).first().waitFor({ state: "hidden" });
  await screen.getByRole("button", "Refresh provider status", { exact: false }).first().waitFor();
  await app.screenshot("positional-prompt-status");
  await expect.poll(() => browser.evaluate(() => document.body.innerText)).toContain("Pi launch arguments cannot include positional prompt 'fix the bug'.");
});
