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

test.beforeEach(async ({ page }) => {
  await installMockApi(page);
});

test("certificate object renders the resourceCertificate projection", async ({ page }) => {
  await page.goto("/objects/obj-cer-0001");

  await expect(page.getByText("Serial number")).toBeVisible();
  await expect(page.getByText("02C6C7")).toBeVisible();
  await expect(page.getByText("CN=A90DC5BE, serialNumber=0E65A4F5")).toBeVisible();
  await expect(page.getByText("CN=A9114E750000, serialNumber=5A179648")).toBeVisible();
  await expect(page.getByText("5a179648b3ef2369dce7bdb58140ff7dc7060abf").first()).toBeVisible();
  await expect(page.getByText("rsync://repo-a.example/ca1/main.crl")).toBeVisible();
  // IP resources rendered from the real serde shape (prefix + range, v4 + v6)
  await expect(page.getByText("103.152.34.0/23")).toBeVisible();
  await expect(page.getByText("192.0.2.0 - 192.0.2.255")).toBeVisible();
  await expect(page.getByText("2606:6800::/32")).toBeVisible();
  // AS resources rendered as AS chips (Id + Range)
  await expect(page.getByText("AS38008", { exact: true })).toBeVisible();
  await expect(page.getByText("AS65536-AS65551")).toBeVisible();
  await expect(page.getByText("1.2.840.113549.1.1.11")).toBeVisible();
});

test("certificate AS resources collapse behind an expandable '+N more' toggle", async ({ page }) => {
  await page.goto("/objects/obj-cer-0001");

  // 15 AS entries: 12 visible, rest behind the toggle.
  const moreToggle = page.getByRole("button", { name: "+3 more" });
  await expect(moreToggle).toBeVisible();
  await expect(page.getByText("AS38008", { exact: true })).toBeVisible();
  await expect(page.getByText("AS64507", { exact: true })).not.toBeVisible();

  await moreToggle.click();
  await expect(page.getByText("AS64507", { exact: true })).toBeVisible();
  await expect(page.getByText("AS64496", { exact: true })).toBeVisible();

  const lessToggle = page.getByRole("button", { name: "Show less" });
  await expect(lessToggle).toBeVisible();
  await lessToggle.click();
  await expect(page.getByText("AS64507", { exact: true })).not.toBeVisible();
});

test("crl object renders header fields from the top-level payload", async ({ page }) => {
  await page.goto("/objects/obj-crl-0001");

  await expect(page.getByText("CN=A9114E750000, serialNumber=5A179648")).toBeVisible();
  await expect(page.getByText("CRL number")).toBeVisible();
  await expect(page.getByText("0D42")).toBeVisible();
  await expect(page.getByText("5a179648b3ef2369dce7bdb58140ff7dc7060abf")).toBeVisible();
  // Revoked certificates table from the sub-endpoint
  await expect(page.getByRole("cell", { name: "1B0B0AD86D78E77D1491B5069E6287D08EEA7C17" })).toBeVisible();
  await expect(page.getByRole("cell", { name: "6A106263F88133615B7E8D3466C192CEC0904F2D" })).toBeVisible();
});
