import * as NodeHttp from "node:http";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Explicit slashless HTTP and HTTPS addresses navigate", WEB, async ({ t3, app, browser, screen }) => {
  let hits = 0;
  const server = NodeHttp.createServer((request, response) => {
    if (request.url === "/health") hits++;
    response.writeHead(200, { "content-type": "text/html; charset=utf-8" });
    response.end('<!doctype html><title>Health fixture reached</title><body style="font:32px sans-serif;padding:32px">Health fixture reached</body>');
  });
  await new Promise<void>((resolve) => server.listen(44605, "127.0.0.1", resolve));
  try {
    const project = await t3.project("preview-demo");
    const thread = await t3.thread({ project, title: "Preview URL check", messages: ["first"] });
    await t3.open(thread);
    await screen.getByRole("button", "Toggle right panel").tap();
    await screen.getByRole("button", "Browser B").tap();
    const input = screen.getByRole("textbox", "Search or enter URL");
    await input.fill("http:127.0.0.1:44605/health");
    await browser.keyboard.press("Enter");
    await screen.getByText(/Health fixture reached|Could not open this address/).waitFor();
    await screen.getByRole("button", "Refresh", { exact: true }).waitFor();
    await browser.evaluate(async () => {
      await Promise.all(document.getAnimations().filter((a) => a.effect?.getTiming().iterations !== Infinity).map((a) => a.finished.catch(() => {})));
    });
    await input.tap();
    await app.screenshot("slashless-http");
    const httpHits = hits;

    if (await browser.evaluate(() => !!document.querySelector('[aria-label="Dismiss notification"]'))) {
      await screen.getByRole("button", "Dismiss notification").first().tap();
    }
    await input.fill("http://127.0.0.1:44605/health");
    await browser.keyboard.press("Enter");
    await screen.getByText("Health fixture reached", { exact: true }).waitFor();
    await screen.getByRole("button", "Refresh", { exact: true }).waitFor();
    await browser.evaluate(async () => {
      await Promise.all(document.getAnimations().filter((a) => a.effect?.getTiming().iterations !== Infinity).map((a) => a.finished.catch(() => {})));
    });
    await app.screenshot("standard-http-control");

    await input.fill("https:example.com/path");
    await browser.keyboard.press("Enter");
    await screen.getByText(/Example Domain|Could not open this address/).first().waitFor();
    await screen.getByRole("button", "Refresh", { exact: true }).waitFor();
    await browser.evaluate(async () => {
      await Promise.all(document.getAnimations().filter((a) => a.effect?.getTiming().iterations !== Infinity).map((a) => a.finished.catch(() => {})));
    });
    await input.tap();
    await app.screenshot("slashless-https");
    const httpsValue = await browser.evaluate(() => (document.querySelector('input[placeholder="Search or enter URL"]') as HTMLInputElement)?.value);
    expect({ httpNavigated: httpHits > 0, httpsValue }).toEqual({
      httpNavigated: true,
      httpsValue: "https://example.com/path",
    });
  } finally {
    await new Promise<void>((resolve) => server.close(() => resolve()));
  }
});
