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

test("Home-relative provider Binary paths detect the same executable as absolute paths", WEB, async ({t3, app, screen, browser}) => {
  await t3.project("Binary path proof");
  await t3.open();
  await screen.getByRole("textbox").waitFor({timeout: 120_000});
  await app.open("/settings/providers");
  const refresh = async () => {
    await screen.getByRole("button", /Refresh provider status/).tap();
    await browser.locator('button[aria-busy="true"]').waitFor({state:"hidden"});
  };
  const detail = browser.locator("[data-provider-detail]");
  for (const provider of [
    {name:"Kiro", binary:"kiro-cli", version:"v2.32.0", label:"kiro"},
    {name:"fx", binary:"fx", version:"v0.0.13", label:"fx"},
    {name:"Antigravity", binary:"agy", version:null, label:"antigravity"},
  ]) {
    await screen.getByRole("button", `Select ${provider.name}`).tap();
    await refresh();
    if (provider.version) await expect.soft(detail).toContainText(provider.version);
    else await expect.soft(screen.getByText("Installed.", {exact:true})).toBeVisible();
    await app.screenshot(`${provider.label}-home-relative`);
    const binary = screen.getByRole("textbox", "Binary path");
    await binary.waitFor();
    // Mask the synthetic absolute home in the recording and control captures.
    await browser.evaluate(() => {
      const input = document.querySelector<HTMLInputElement>('input[aria-label="Binary path"]');
      if (input) input.type = "password";
    });
    const maskedBinary = browser.locator('input[aria-label="Binary path"]');
    await maskedBinary.fill(`${process.env.T3_PROOF_HOME}/.local/bin/${provider.binary}`);
    await maskedBinary.press("Tab");
    await refresh();
    if (provider.version) await detail.getByText(provider.version, {exact:true}).waitFor();
    else await screen.getByText("Installed.", {exact:true}).waitFor();
    await app.screenshot(`${provider.label}-absolute-control`);
    await maskedBinary.fill(`~/.local/bin/${provider.binary}`);
    await maskedBinary.press("Tab");
    await browser.evaluate(() => {
      const input = document.querySelector<HTMLInputElement>('input[aria-label="Binary path"]');
      if (input) input.type = "text";
    });
  }
});
