import { expect } from "vitest";

export function anonymousFormFields(container: ParentNode) {
  return Array.from(
    container.querySelectorAll("input, select, textarea"),
  ).filter(
    (field) =>
      !field.getAttribute("id")?.trim() && !field.getAttribute("name")?.trim(),
  );
}

export function expectNoAnonymousFormFields(container: ParentNode) {
  const fields = anonymousFormFields(container);
  const diagnostics = fields.map((field) => {
    const tag = field.tagName.toLowerCase();
    const type = field.getAttribute("type");
    const label =
      field.getAttribute("aria-label") ||
      field.getAttribute("placeholder") ||
      field.getAttribute("data-testid") ||
      field.textContent ||
      "";
    return `${tag}${type ? `[type=${type}]` : ""}${label ? `: ${label}` : ""}`;
  });

  expect(fields, diagnostics.join("\n")).toHaveLength(0);
}
