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

test("Enter validates and advances Add provider identity", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("provider-identity");
  const failures: unknown[] = [];
  const check = async (action: () => Promise<void>, label: string) => {
    try { await action(); } catch (error) { failures.push(error); }
    finally { await app.screenshot(label); }
  };
  const openIdentity = async () => {
    await t3.open("/settings/providers");
    await screen.getByRole("button", "Add provider", { exact: true }).first().tap();
    await screen.getByRole("button", "Configure manually", { exact: true }).tap();
    await screen.getByLabel("Label", { exact: true }).fill("Work");
  };
  await openIdentity();
  await screen.getByLabel("Instance ID", { exact: true }).fill("bad id!");
  await browser.keyboard.press("Enter");
  await check(() => expect(screen.getByRole("alert")).toHaveText("Instance ID must start with a letter and use only letters, digits, '-', or '_'."), "invalid-id-enter");
  await screen.getByRole("button", "Next", { exact: true }).tap();
  await app.screenshot("invalid-id-click-control");
  await screen.getByLabel("Instance ID", { exact: true }).fill("codex_work");
  await browser.keyboard.press("Enter");
  await check(() => expect(screen.getByRole("button", "Add instance", { exact: true })).toBeVisible(), "instance-id-enter");
  await browser.keyboard.press("Escape");
  await openIdentity();
  await screen.getByLabel("Instance ID", { exact: true }).fill("codex_work");
  await screen.getByLabel("Label", { exact: true }).tap();
  await browser.keyboard.press("Enter");
  await check(() => expect(screen.getByRole("button", "Add instance", { exact: true })).toBeVisible(), "label-enter");
  await browser.keyboard.press("Escape");
  await t3.open("/settings/providers");
  await screen.getByRole("button", "Add provider", { exact: true }).first().tap();
  await screen.getByRole("button", "Show more", { exact: true }).tap();
  await screen.getByRole("button", "Add Kilo", { exact: true }).tap();
  await screen.getByLabel("Label", { exact: true }).tap();
  await browser.keyboard.press("Enter");
  await check(() => expect(screen.getByRole("button", "Skip for now", { exact: true })).toBeVisible(), "acp-label-enter");
  if (failures.length) throw failures[0];
});
