import { expect, test } from "@playwright/test";

const screenshotRoot = "../../../../specs/develop/20260623_2/m6_full_e2e";

test("renders live overview data from query service without global object list", async ({ page }) => {
  const apiRequests: string[] = [];
  const consoleErrors: string[] = [];

  page.on("request", (request) => {
    const url = new URL(request.url());
    if (url.pathname.startsWith("/api/v1")) {
      apiRequests.push(`${url.pathname}${url.search}`);
    }
  });
  page.on("console", (message) => {
    if (message.type() === "error") {
      consoleErrors.push(message.text());
    }
  });

  await page.goto("/");

  await expect(page.getByRole("heading", { name: "Global RPKI validation health" })).toBeVisible();
  await expect(page.getByText(/run_\d+/).first()).toBeVisible();
  await expect(page.getByText("Top repositories by workload")).toBeVisible();
  await expect(page.locator(".issue-card").first()).toBeVisible();

  expect(apiRequests).toContain("/api/v1/latest_run");
  expect(apiRequests.some((request) => request.includes("/stats/object-types"))).toBe(true);
  expect(apiRequests.some((request) => request.includes("/stats/validation"))).toBe(true);
  expect(apiRequests.some((request) => request.includes("/stats/reasons"))).toBe(true);
  expect(apiRequests.some((request) => request.includes("/repos?limit=8"))).toBe(true);
  expect(apiRequests.some((request) => request.includes("/objects?"))).toBe(false);
  expect(consoleErrors).toEqual([]);

  await page.screenshot({ path: `${screenshotRoot}/rpki-explorer-overview-live.png`, fullPage: true });
});
