import { describe, expect, it } from "vitest";

import { buildSearchQueryVariants, compact1688SearchKeyword } from "./search-query-utils.js";

describe("compact1688SearchKeyword", () => {
  it("normalizes and removes all whitespace before executing 1688 searches", () => {
    expect(compact1688SearchKeyword(" 岩棉板  \nTR10\t容重100kg　 ")).toBe("岩棉板TR10容重100kg");
    expect(compact1688SearchKeyword(null)).toBe("");
  });
});

describe("buildSearchQueryVariants", () => {
  it("keeps only the compact keyword for strict configured stages", () => {
    expect(buildSearchQueryVariants("球墨铸铁井盖 Φ700重型", { strict: true })).toEqual([
      "球墨铸铁井盖Φ700重型",
    ]);
  });

  it("returns only compact variants for default progressive search", () => {
    const variants = buildSearchQueryVariants(" 球墨铸铁井盖 \n通用\tΦ700重型　 ");

    expect(variants).toEqual([
      "球墨铸铁井盖通用Φ700重型",
      "球墨铸铁井盖Φ700重型",
      "球墨铸铁井盖",
    ]);
    expect(variants.every((variant) => !/[\s\u3000]/u.test(variant))).toBe(true);
  });
});
