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

test("Browser opens absolute localhost names as addresses", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  await t3.open("/settings/providers");
  await screen.getByText("Replay (dev)", { exact: true }).waitFor();
  const thread = await t3.thread({ project, title: "Localhost addresses", messages: ["first"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Browser B").tap();
  const address = screen.getByRole("textbox", "Search or enter URL");
  const actual: string[] = [];
  for (const [input, label, target] of [
    ["localhost./health", "absolute-localhost-path", "http://localhost./health"],
    ["localhost.?debug=1", "absolute-localhost-query", "http://localhost./?debug=1"],
    ["localhost/health", "plain-localhost-control", "http://localhost/health"],
    ["example.com./health", "dotted-host-control", "https://example.com./health"],
  ]) {
    await address.fill(input!);
    await browser.keyboard.press("Enter");
    const search = `https://duckduckgo.com/?q=${encodeURIComponent(input!)}`;
    await browser.locator(`input[data-preview-url-input][value="${target}"], input[data-preview-url-input][value="${search}"]`).waitFor();
    await screen.getByRole("button", "Refresh", { exact: true }).waitFor();
    actual.push(await browser.evaluate(() => document.querySelector<HTMLInputElement>('input[aria-label="Search or enter URL"]')!.value));
    await app.screenshot(label!);
  }
  expect(actual.slice(0, 2)).toEqual(["http://localhost./health", "http://localhost./?debug=1"]);
});
