import { test, expect } from "@playwright/test";
import { USERS, login } from "./helpers";
import { vendorWithCode, closeDb } from "./fixtures";

/**
 * Issue #109 — the New PO screen vendor field is a searchable combobox that matches
 * by vendor name AND code (mirrors the items search).
 */
test.afterAll(closeDb);

test("#109 new PO vendor field is a searchable combobox (name + code)", async ({ page }) => {
  const vendor = await vendorWithCode();
  test.skip(!vendor, "no vendor with a code in staging data");

  await login(page, USERS.MANAGER);
  await page.goto("/po/new");

  // Open the vendor combobox (trigger shows the "No vendor selected" placeholder).
  await page.getByText("No vendor selected").click();

  const search = page.getByPlaceholder(/Search by name or code/i);
  await expect(search).toBeVisible();

  // Searching by the vendor's CODE surfaces the vendor by name — proving code search.
  await search.fill(vendor!.vendorId!);
  await expect(page.getByText(vendor!.name, { exact: false }).first()).toBeVisible();
});
